Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust is eagerly evaluating range causing inconsistencies #72763

Closed
eloraiby opened this issue May 30, 2020 · 4 comments
Closed

Rust is eagerly evaluating range causing inconsistencies #72763

eloraiby opened this issue May 30, 2020 · 4 comments

Comments

@eloraiby
Copy link

Following this small example:

#[cfg(test)]
mod tests {
    #[test]
    fn test_for() {
        let mut v = Vec::new();
        for i in 0..10 {
            v.push(i);
        }

        for i in 0..v.len() {
            if i < 100 {
                v.push(i);
            }
        }

        assert!(v.len() == 110); // Crash! expected 110, count is 20
    }

    #[test]
    fn test_loop() {
        let mut v = Vec::new();
        for i in 0..10 {
            v.push(i);
        }

        let mut i = 0;
        loop {
            if i >= v.len() {
                break;
            }

            if i < 100 {
                v.push(i);
            }

            i += 1;
        }

        assert!(v.len() == 110);
    }

}

If that's the normal behaviour, it should be documented in https://doc.rust-lang.org/std/ops/struct.Range.html, in the language references and tutorials.

The behaviour as it stands is in contrast of what other languages implements and is confusing.

@jonas-schievink
Copy link
Contributor

Nothing in Rust is lazily evaluated. Evaluation order of language constructs like for and the range syntax is in scope of https://github.com/rust-lang/reference/.

@eloraiby
Copy link
Author

@jonas-schievink you failed to provide where in that reference! - if it's in there -

@jonas-schievink
Copy link
Contributor

It isn't documented yet – but this is being tracked in rust-lang/reference#248

@eloraiby
Copy link
Author

thanks @jonas-schievink

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants