Skip to content

Perculiar behaviour when parsing 0..<ident> range expressions in for loops #20858

Closed
@brendanzab

Description

@brendanzab
fn main() {
    let n = 2;
    for x in 0..n {
        println!("Hello, world!")
    }
}
<anon>:5:16: 5:17 error: expected `:`, found `!`
<anon>:5         println!("Hello, world!")
                        ^

http://is.gd/pOrPMl

Using parentheses fixes the issue: for x in (0..n) {

Interestingly this works:

struct Foo { octaves: usize }

fn main() {
    let foo = Foo { octaves: 2 };
    for x in 0..foo.octaves {
        println!("Hello, world!")
    }
}

http://is.gd/3Rnvx2

This was encountered in noise-rs.

cc. @amaranth @Cifram

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