Skip to content

for: replace range() function with range notation #389

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

Closed
genbattle opened this issue Jan 11, 2015 · 6 comments
Closed

for: replace range() function with range notation #389

genbattle opened this issue Jan 11, 2015 · 6 comments

Comments

@genbattle
Copy link

I was updating old code of mine and got this warning for one of my code files:

main.rs:15:14: 15:19 warning: use of unstable item: will be replaced by range notation, #[warn(unstable)] on by default
main.rs:15     for i in range(1, 1000) {

I pulled up Rust by Example to see what the new idiomatic way of doing this was and found that Rust by Example was using the same (apparently unstable) syntax. The Rust book also uses the outdated syntax.

So at the moment there's no straightforward up-to-date resources on what the syntax should be. But apparently it involves Rust's new [a..b] range syntax. Can someone with more knowledge about what the syntax should be please update Rust by Example.

@genbattle
Copy link
Author

It looks like this is a fairly new change. I also discovered by wading through various github issues for Rust that the new syntax for basic for loops using numeric ranges is:

for i in a..b {

@genbattle
Copy link
Author

Also apparently brackets are required in some cases but not in others?

for i in (a..b) {

@vayn
Copy link

vayn commented Jan 21, 2015

I encountered this warning in Rust book, too. Thanks for sharing the information @genbattle

@mdinger
Copy link
Contributor

mdinger commented Jan 21, 2015

for i in (a..b) {is just a bug. I think rust-lang/rust#21396 is a fix. I think I fixed this in #421 if it's accepted.

@anderspitman
Copy link

Is there any way to avoid this warning when using a variable in the range? ie

let a = [i32; 10];
for i in range(0, a.len()) {}

@mdinger
Copy link
Contributor

mdinger commented Jan 25, 2015

fn main() {
    let a: [i32; 2] = [1, 2] ;
    for i in 0..a.len() {
        println!("{:?}", i);
    }
}

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

4 participants