我遇到了一些Rust代碼的問題。我有一個非常簡單的功能,但將在代碼中的錯誤,這似乎是不相關的:匹配字符串文字時,「字符大小沒有實現」字符?
use std::env::Args;
fn without_xxx(args: Args) -> Vec<String>{
let mut out: Vec<String> = vec![];
let mut xxx = false;
for arg in args{
match &arg{
"-" => xxx=true, //this line
_ => out.push(arg.to_string())
}
}
return out;
}
如果您註釋掉標線,會出現顯示任何錯誤。然而,與簡單的線條,它揭示了這個神祕的錯誤集:
<anon>:7:9: 12:10 error: the trait `core::marker::Sized` is not implemented for the type `str` [E0277]
<anon>: 7 for arg in args{
<anon>: 8 match &arg{
<anon>: 9 "-" => xxx=true,
<anon>:10 _ => out.push(arg.to_string())
<anon>:11 }
<anon>:12 }
<anon>:7:9: 12:10 help: see the detailed explanation for E0277
<anon>:7:9: 12:10 note: `str` does not have a constant size known at compile-time
<anon>:7:9: 12:10 note: required by `core::option::Option::Some`
<anon>:7:13: 7:16 error: the trait `core::marker::Sized` is not implemented for the type `str` [E0277]
<anon>:7 for arg in args{
^~~
<anon>:7:13: 7:16 help: see the detailed explanation for E0277
<anon>:7:13: 7:16 note: `str` does not have a constant size known at compile-time
<anon>:7:13: 7:16 note: all local variables must have a statically known size
<anon>:7:9: 12:10 error: type mismatch resolving `<std::env::Args as core::iter::Iterator>::Item == str`:
expected struct `collections::string::String`,
found str [E0271]
<anon>: 7 for arg in args{
<anon>: 8 match &arg{
<anon>: 9 "-" => xxx=true,
<anon>:10 _ => out.push(arg.to_string())
<anon>:11 }
<anon>:12 }
<anon>:7:9: 12:10 help: see the detailed explanation for E0271
感謝您的提示:) – Earlz
我可能是錯誤的,但不是最好使用'&* x'過'&X [..]'現在? – Veedrac
@Veedrac好點!我不確定*哪一種比較習慣。我認爲我在「1.0之前的日子」中選擇了'&x [..]',之後就沒有想太多了。兩者都應該編譯成相同的代碼,所以這是一個看起來更明顯的問題,我猜。 – Shepmaster