Skip to content

Redefining range in this rust_book example may be confusing #21577

Closed
@ghost

Description

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

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