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
Comments
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. |
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. |
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. |
@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 cc @steveklabnik ? |
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 Thanks for a really great presentation and also for taking feedback seriously, _Greg |
@GregDavidson I was under the impression that when you were talking about shadowing within the same block, that you meant shadowing within
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 :-) |
As an aside, this reminds me of static single assignment (SSA) of LLVM IR. |
Shadowing is documented in the book, so I'm going to give this a close. |
I was taken aback by this code in first.rs:
which appears to be equivalent to:
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!
The text was updated successfully, but these errors were encountered: