rust - 尽管有足够的信息,但仍需要类型注释
问题描述
我想我会利用 Advent of Code 的机会最终尝试学习 Rust。当我尝试为第 1 天实施解决方案时,我最初尝试过:
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
s的范围求和,那不应该i32
默认给我一个吗?
解决方案
推荐阅读
- python - 如何从Python中的一系列图像生成一系列(x,y)坐标?
- c++ - 具有循环关系的类型的解决方法是什么?
- firebase - Firebase 托管:如何在同一域上托管两个 Web 应用程序作为子目录
- amazon-web-services - AWS SES、S3 电子邮件备份
- c++ - 如何在 cpp 中嵌入一个 linux 命令示例 ln -l path1 path2
- c++ - Emacs 公司模式不支持 C++ STL 函数的自动完成
- javascript - 半自动 Google 表格发票
- ftp - Mule 的 FTP 连接问题
- sass - Angular Material 更改应用程序背景和前景主题
- mapbox - 如何使用 mapbox gl 从现有图层获取 geojson 边界数组?