Skip to content

Redefining range in this rust_book example may be confusing #21577

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
ghost opened this issue Jan 24, 2015 · 0 comments · Fixed by #22296
Closed

Redefining range in this rust_book example may be confusing #21577

ghost opened this issue Jan 24, 2015 · 0 comments · Fixed by #22296

Comments

@ghost
Copy link

ghost commented Jan 24, 2015

here: http://doc.rust-lang.org/book/iterators.html
this example will redefine the function range:

let mut range = range(0, 10);

loop {
    match range.next() {
        Some(x) => {
            println!("{}", x);
        },
        None => { break }
    }
}

so if I add the following(in the same main.rs):

    let nums = vec![1, 2, 3];
    for x in range(0, 10) {
        println!("{}", x);
    }

the error is:

src/main.rs:19:14: 19:26 error: expected function, found `core::iter::Range<_>`
src/main.rs:19     for x in range(0, 10) {

Is there a way to turn on a warning when such overrides happen? Something that says that this line "overwrites" previous definition of "range"(and maybe point to previous definition too), even though it's true only for a limited(local) scope. (tried to look at rustc -W help)

To avoid confusion, I suggest using a different binding name in that example.
Thoughts?

Thanks.

EDIT:
12 places where range is a binding thus overriding the function with the same name:
https://github.com/rust-lang/rust/search?utf8=%E2%9C%93&q=%22let+mut+range%22&type=Code
https://github.com/rust-lang/rust/search?utf8=%E2%9C%93&q=%22let+range%22&type=Code

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

Successfully merging a pull request may close this issue.

1 participant