rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.88k stars 12.67k forks source link

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

Closed brendanzab closed 9 years ago

brendanzab commented 9 years ago
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

brendanzab commented 9 years ago

Ooops, this is probably a dupe of #20830 - I was searching for "range expression" not "range notation".

huonw commented 9 years ago

Closing as a dupe of #20830, thanks for filing.