3 ms·
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 f
by cphoover 8y ago
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.
- bluejekyll 8y agoTwo 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.
- gpm 8y agoI'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] }
- Thiez 8y agoYou forgot `match` and closures. But in general I think new variables can be created in all contexts where a pattern can be used.
- steveklabnik 8y agoYes, in fact, it's always a pattern. Even let x = 5; is a pattern, just a very simple one!
- zackpierce 8y agoThe 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.
- int_19h 8y agoIMO, it should scare you less than mutating the variable, which is the equivalent pattern in most other languages.
- deleted 8y ago[deleted]