【发布时间】:2018-12-19 08:56:03
【问题描述】:
下面的代码读取数字,对它们求和,然后打印总和。我尝试了一些注释,但没有成功。我肯定错过了什么。我怎样才能让它工作?
use std::io;
use std::io::Read;
fn main() {
let mut buff = String::new();
io::stdin().read_to_string(&mut buff).expect("read_to_string error");
let v: i32 = buff
.split_whitespace()
.filter_map(|w| w.parse().ok())
.sum();
println!("{:?}", v);
}
来自编译器的错误消息:
type annotations needed
--> src\main.rs:9:10
|
9 | .filter_map(|w| w.parse().ok())
| ^^^^^^^^^^ cannot infer type for `B`
【问题讨论】:
标签: parsing types rust type-inference