我想我会利用代码出现的机会来尝试学习Rust。当我尝试实施第一天的解决方案时,我最初尝试:
use std::io::{self, BufRead};
fn main() {
let tot = io::stdin().lock().lines()
.map(|l| l.unwrap())
.map(|s| s.parse::<i32>().unwrap())
.map(|i| i / 3 - 2)
.sum();
println!("{}", tot);
}
但这失败并显示以下错误:
error[E0282]: type annotations needed
--> src/main.rs:4:9
|
4 | let tot = io::stdin().lock().lines()
| ^^^ consider giving `tot` a type
error: aborting due to previous error
将声明更改为let tot:i32 = [...]
并已成功完成。
但是为什么在那里需要类型注释?我只是在总结i32
的范围,不是默认情况下只给我一个i32
吗?