是否可以在 Rust for 循环中声明变量的类型? [英] Is it possible to declare the type of the variable in Rust for loops?
本文介绍了是否可以在 Rust for 循环中声明变量的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C++ 示例:
for (long i = 0; i < 101; i++) {
//...
}
在 Rust 中我尝试过:
In Rust I tried:
for i: i64 in 1..100 {
// ...
}
我可以很容易地在 for 循环之前声明一个 let i: i64 =
var但我宁愿学习正确的方法来做到这一点,但这导致
I could easily just declare a let i: i64 =
var before the for loop
but I'd rather learn the correct way to doing this, but this resulted in
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
推荐答案
您可以使用 整数后缀 在您在该范围内使用的文字之一上.类型推断将完成剩下的工作:
You can use an integer suffix on one of the literals you've used in the range. Type inference will do the rest:
for i in 1i64..101 {
println!("{}", i);
}
这篇关于是否可以在 Rust for 循环中声明变量的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文