Skip to content

range notation with for expression for i in 0 .. n {} should be parsed as for i in (0 .. n) {} #20921

@mandel59

Description

@mandel59

The problem is that for i in 0 .. n {} is ambiguous between for i in (0 .. n) {} and for i in 0 .. (n {}) for consistency.

fn main() {
    let n = 10;

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

This code yields:

% rustc ambiguous.rs
ambiguous.rs:5:16: 5:17 error: expected `:`, found `!`
ambiguous.rs:5         println!("{}", i);
                              ^

While these codes pass:

fn main() {
    for i in 0..10 {
        println!("{}", i);
    }
}
fn main() {
    let mut n = 0..10;
    for i in n {
        println!("{}", i);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions