我应该如何在Rust(错误[E0282])中键入annotate collect()?
以下是一个修改后的示例:我应该如何在Rust(错误[E0282])中键入annotate collect()?,rust,Rust,以下是一个修改后的示例: .collect()在此上下文中的某个位置需要类型注释。如果它无法从变量的注释中获取它(当它隐藏在展开后面时无法获取),则需要使用turbofish样式添加类型注释。以下代码有效:() fn main(){ //让字符串=vec![“豆腐”、“93”、“18”]; 让strings=vec![“93”,“18”]; 让可能的数字=字符串 .into_iter() .map(|s | s.parse::()) 收集::() .unwrap(); println!(“结果:
.collect()
在此上下文中的某个位置需要类型注释。如果它无法从变量的注释中获取它(当它隐藏在展开后面时无法获取),则需要使用turbofish样式添加类型注释。以下代码有效:()
fn main(){
//让字符串=vec![“豆腐”、“93”、“18”];
让strings=vec![“93”,“18”];
让可能的数字=字符串
.into_iter()
.map(|s | s.parse::())
收集::()
.unwrap();
println!(“结果:{:?}”,可能的_数);
}
编辑:有关turbofish操作员的更多信息,请参见
fn main() {
// let strings = vec!["tofu", "93", "18"];
let strings = vec!["93", "18"];
let possible_numbers: Result<Vec<i32>, std::num::ParseIntError> = strings
.into_iter()
.map(|s| s.parse::<i32>())
.collect();
let possible_numbers = possible_numbers.unwrap();
// [93, 18]
println!("Results: {:?}", possible_numbers);
}
fn main() {
// let strings = vec!["tofu", "93", "18"];
let strings = vec!["93", "18"];
let possible_numbers = strings
.into_iter()
.map(|s| s.parse::<i32>())
.collect::<Result<Vec<i32>, std::num::ParseIntError>>()
.unwrap();
println!("Results: {:?}", possible_numbers);
}