【发布时间】:2019-09-21 17:25:24
【问题描述】:
这个问题可能看起来非常基本,但我很难弄清楚如何做到这一点。我有一个整数,需要用for循环循环整数次。
首先,我试过了——
fn main() {
let number = 10; // Any value is ok
for num in number {
println!("success");
}
}
这会打印错误
error[E0277]: `{integer}` is not an iterator
--> src/main.rs:3:16
|
3 | for num in number{
| ^^^^^^ `{integer}` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `{integer}`
= note: if you want to iterate between `start` until a value `end`, use the exclusive range syntax `start..end` or the inclusive range syntax `start..=end`
= note: required by `std::iter::IntoIterator::into_iter`
接下来,我试了——
fn main() {
let number = 10; // Any value is ok
for num in number.iter() {
println!("success");
}
}
编译器说整数没有方法 iter
error[E0599]: no method named `iter` found for type `{integer}` in the current scope
--> src/main.rs:3:23
|
3 | for num in number.iter() {
| ^^^^
我该怎么做?
【问题讨论】:
标签: for-loop rust iterator integer