< 返回版块

suphgcm 发表于 2022-05-03 22:04

源代码:

fn main() {

let a = [10, 20, 30, 40, 50];

for item in a {
    println!("The value is {}", item);
}

}

Linux环境:

ubuntu:~/rust/loops$ cargo run

Compiling loops v0.1.0 (/home/suph/rust/loops)

Finished dev [unoptimized + debuginfo] target(s) in 11.26s

 Running `target/debug/loops`

The value is 10

The value is 20

The value is 30

The value is 40

The value is 50

Window环境:

PS E:\Project\rust\loops> cargo run

Compiling loops v0.1.0 (E:\Project\rust\loops)

error[E0277]: [{integer}; 5] is not an iterator

--> src\main.rs:4:17

|

4 | for item in a {

| ^ borrow the array with & or call .iter() on it to iterate over it

|

= help: the trait Iterator is not implemented for [{integer}; 5]

= note: arrays are not iterators, but slices like the following are: &[1, 2, 3]

= note: required because of the requirements on the impl of IntoIterator for [{integer}; 5]

= note: required by into_iter

error: aborting due to previous error

For more information about this error, try rustc --explain E0277.

error: could not compile loops

To learn more, run the command again with --verbose.

PS E:\Project\rust\loops>

评论区

写评论
苦瓜小仔 2022-05-03 22:10

因为你的 Win 的 rustc 版本太旧:https://stackoverflow.com/questions/64018817/why-can-a-for-loop-in-rust-iterate-over-a-slice-or-iterator-but-not-an-array

1 共 1 条评论, 1 页