Re:Thoughts on Rust, a few thousand lines in
by cphoover on Wednesday January 09, 2019 @04:09PM
(#18868904)
I've not done any rust dev before but the whole variable shadowing thing really scares me, in that it makes it not obvious where the initial declaration comes from. This seems difficult to reason about.
[ Parent
| on HN ]
Re:Thoughts on Rust, a few thousand lines in
by bluejekyll on Wednesday January 09, 2019 @04:12PM
(#18868935)
Two things, 1) the compiler doesn’t really allow you to screw it up in really scary ways, but you can I suppose. 2) it allows you to rebind a variable name to a new type, of a new state during execution without coming up with a hokey name for the new thing that’s really just the original modified in some way.
Re:Thoughts on Rust, a few thousand lines in
by int_19h on Wednesday January 09, 2019 @08:00PM
(#18870620)
IMO, it should scare you less than mutating the variable, which is the equivalent pattern in most other languages.
Re:Thoughts on Rust, a few thousand lines in
by gpm on Wednesday January 09, 2019 @04:32PM
(#18869140)
I've done a fair bit of rust development and never found that to be an issue.
It probably helps that the only ways to introduce new variables are all very distinctive:
let [new variables] = [expr];
if/while let [new variables] = [expr] { [new variables scoped here] }
for [new variables] in expr { [new variables scoped here] }
fn name([new_variables]) { [new variables scoped here] }
Re:Thoughts on Rust, a few thousand lines in
by Thiez on Wednesday January 09, 2019 @04:50PM
(#18869294)
You forgot `match` and closures. But in general I think new variables can be created in all contexts where a pattern can be used.
Re:Thoughts on Rust, a few thousand lines in
by steveklabnik on Wednesday January 09, 2019 @04:55PM
(#18869341)
Yes, in fact, it's always a pattern. Even let x = 5; is a pattern, just a very simple one!
Re:Thoughts on Rust, a few thousand lines in
by zackpierce on Wednesday January 09, 2019 @04:44PM
(#18869244)
The good news is that if you want to disallow shadowing in your codebase, there are optional clippy lints that can be applied at compile-time.