gpt4 book ai didi

rust - 是否可以在 Rust for 循环中声明变量的类型?

转载 作者:行者123 更新时间:2023-11-29 07:41:25 24 4
gpt4 key购买 nike

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/24463655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com