Skip to content

Rust 'const iter' results in 'for'-errors #27639

Closed
@oberien

Description

@oberien

having the following code

fn main() {
    const iter: i32 = 5;

    for i in 0..10 {
        println!("{}", i);
    }
}

produces the following error:

src/main.rs:4:5: 6:6 error: type mismatch resolving ` as core::iter::IntoIterator>::IntoIter == i32`:
 expected struct `core::ops::Range`,
    found i32 [E0271]
src/main.rs:4     for i in 0..10 {
src/main.rs:5         println!("{}", i);
src/main.rs:6     }
note: in expansion of for loop expansion
src/main.rs:4:5: 6:6 note: expansion site
src/main.rs:4:5: 6:6 help: run `rustc --explain E0271` to see a detailed explanation
src/main.rs:4:5: 6:6 error: the trait `core::iter::Iterator` is not implemented for the type `i32` [E0277]
src/main.rs:4     for i in 0..10 {
src/main.rs:5         println!("{}", i);
src/main.rs:6     }
note: in expansion of for loop expansion
src/main.rs:4:5: 6:6 note: expansion site
src/main.rs:4:5: 6:6 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:4:5: 6:6 note: `i32` is not an iterator; maybe try calling `.iter()` or a similar method
src/main.rs:4     for i in 0..10 {
src/main.rs:5         println!("{}", i);
src/main.rs:6     }
note: in expansion of for loop expansion
src/main.rs:4:5: 6:6 note: expansion site
src/main.rs:4:5: 6:6 error: the trait `core::iter::Iterator` is not implemented for the type `i32` [E0277]
src/main.rs:4     for i in 0..10 {
src/main.rs:5         println!("{}", i);
src/main.rs:6     }
note: in expansion of for loop expansion
src/main.rs:4:5: 6:6 note: expansion site
src/main.rs:4:5: 6:6 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:4:5: 6:6 note: `i32` is not an iterator; maybe try calling `.iter()` or a similar method
src/main.rs:4     for i in 0..10 {
src/main.rs:5         println!("{}", i);
src/main.rs:6     }
note: in expansion of for loop expansion
src/main.rs:4:5: 6:6 note: expansion site
error: aborting due to 3 previous errors

Somehow defining the const 'iter' doesn't let rust build the iterator needed by the for-loop. I know, that renaming the const fixes this issue, but can someone explain this behavior to be, please?

Kind Regards,
BH16

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions