为什么这段代码可以正常工作
fn main() {
let v1 = vec!["lemonade", "lemon", "type", "lid"];
println!("{:?}", v1.iter().filter(|item| item.starts_with("l")).collect::<Vec<_>>());
}
Run Code Online (Sandbox Code Playgroud)
虽然这段代码给我带来了一个错误,但我有点明白为什么这不起作用并且我知道如何修复它,但我真的不明白它返回什么类型,所以我可以将“_”替换为不那个通用的
fn main() {
let v1 = vec!["lemonade", "lemon", "type", "lid"];
println!("{:?}", v1.iter().filter(|item| item.starts_with("l")).collect::<Vec<&str>>());
}
Run Code Online (Sandbox Code Playgroud)
错误
error[E0277]: a value of type `Vec<&str>` cannot be built from an iterator over elements of
type `&&str`
--> src\main.rs:3:69
|
3 | println!("{:?}", v1.iter().filter(|item| item.starts_with("l")).collect::
<Vec<&str>>());
| ^^^^^^^ value of type
`Vec<&str>` cannot be built from `std::iter::Iterator<Item=&&str>`
|
= help: the trait `FromIterator<&&str>` is not implemented for `Vec<&str>`
= …Run Code Online (Sandbox Code Playgroud) rust ×1