C++ 示例:
for (long i = 0; i < 101; i++) {
//...
}
我在 Rust 中尝试过:
for i: i64 in 1..100 {
// ...
}
我可以轻松地在 for 循环之前声明一个 let i: i64 =
var
但我宁愿学习正确的方法来做这件事,但这导致了
error: expected one of `@` or `in`, found `:`
--> src/main.rs:2:10
|
2 | for i: i64 in 1..100 {
| ^ expected one of `@` or `in` here
最佳答案
您可以使用 integer suffix在您在该范围内使用的其中一个文字上。类型推断将完成剩下的工作:
for i in 1i64..101 {
println!("{}", i);
}
关于rust - 是否可以在 Rust for 循环中声明变量的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47377094/