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

let can shadow name in same block??? #11

Closed
GregDavidson opened this issue Jul 29, 2015 · 8 comments
Closed

let can shadow name in same block??? #11

GregDavidson opened this issue Jul 29, 2015 · 8 comments

Comments

@GregDavidson
Copy link

I was taken aback by this code in first.rs:

   Link::More(node) => {
            let node = *node;
            self.head = node.next;
            Some(node.elem)
        }

which appears to be equivalent to:

        Some(node_box) => {
            let node = *node_box;
            self.head = node.next;
            Some(node.elem)
        }

I tried to find something in the official Rust
documentation to justify shadowing a name
which is already in use in the block and didn't
find anything. Can you point to somewhere
which promises that this is stable? And maybe
something should be said to explain this to
newbies like me!

P.S. I'm absolutely loving this tutorial, it's
filling in key things I was missing!

@Gankra
Copy link
Collaborator

Gankra commented Jul 29, 2015

Yes you can absolutely shadow a name, it makes the old name inaccessible for the scope of the new name. This is totally intended and stable.

CC @steveklabnik for if this is actually documented.

@steveklabnik
Copy link

i am 99% sure shadowing is documented, and is used very, very often in Rust code. I don't remember off the top of my head, so leaving this open to make sure.

@GregDavidson
Copy link
Author

I've looked and failed to find anywhere this kind of shadowing is documented. It really needs to be specifically addressed and reflected in at least one example, but then It's cool although likely to throw newbies. As in other languages, the expression which is used to initialize the new name is part of the old scope and then the new scope begins. Unlike most other languages, the new scope is delimited by the same end-of-block that delimits the old scope. Syntactically the new scope is a tail of the old scope but I'm guessing that in terms of lifetimes it is nested inside the old scope, i.e. I'm guessing that any object owned by the shadowed name gets dropped just after anything in the new scope is dropped.

@MatejLach
Copy link

@GregDavidson It's actually documented here and here and there's also this example at rustbyexample.com, (although not in the same block), but perhaps there should be a more explicit example in the Variable Bindings section of the book?

cc @steveklabnik ?

@GregDavidson
Copy link
Author

The third example you cite, from rustbyexample, does indeed show shadowing within the same block - mea culpa since I read that and didn't remember it. The other two links do NOT show any shadowing within the same block. I don't know of another block-oriented language which allows this, although some functional languages do, at least in some modes (I'm thinking of ghci).

Wouldn't it be great if we could attach a popup FAQ gadget to our online articles so that reader questions could be caught and answered without disrupting the main flow? I'm working on things
like that in my (not so copious) spare time!

Thanks for a really great presentation and also for taking feedback seriously,

_Greg

@MatejLach
Copy link

@GregDavidson I was under the impression that when you were talking about shadowing within the same block, that you meant shadowing within { } as in:

fn main() {
 let mut guess = String::new();
 let guess: u32 = guess.trim().parse()
}

That's why I linked to the first two examples, but apparently I misunderstood, sorry about that :-(

And yeah, it is shown in the third example, although I still don't see how that differs from my first two links :-)

@louy2
Copy link
Contributor

louy2 commented Aug 8, 2016

As an aside, this reminds me of static single assignment (SSA) of LLVM IR.

@steveklabnik
Copy link

Shadowing is documented in the book, so I'm going to give this a close.

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

5 participants