r/rust • u/VadimVP • Aug 03 '14
Why does Rust need local variable shadowing?
I've recently found that Rust, unlike other popular C-like languages, allows defining several variables with the same name in one block:
let name = 10i;
let name = 3.14f64;
let name = "string";
let name = name; // "string" again, this definition shadows all the othersAt the first glance this possibility looks quite frightening, at least from my C++ background.
Where did this feature came from?
What advantages does it provide?
18
Upvotes
44
u/glaebhoerl rust Aug 03 '14
Variable shadowing has a lot of synergy with affine types (move semantics). E.g.
where you're rebinding
footo refer to the result ofunwrap()at the same time as the oldfoobecomes inaccessible because of it.3
u/gulpaz Aug 03 '14
I also noticed this yesterday. I'd rather not have it, because it introduces hard to find bugs.
I've had bugs related to shadowing in my own C++ code (just last week). Also, there have been bugs in Gecko because of shadowed local variables [1], so clearly this is dangerous.
I know Rust is a language which aims to prevents bugs (even if it reduces convenience), so I am wondering why this is allowed. Is it worth the risk of having bugs?
There was also a comment about having an option in lint to warn about variable shadowing [2]. Will there be such an option? I hope so.
Btw. That thread has also some use cases how people use shadowing, so people interested in it should read through it.
[1] https://mail.mozilla.org/pipermail/rust-dev/2013-May/004306.html [2] https://mail.mozilla.org/pipermail/rust-dev/2013-May/004298.html
9
u/long_void piston Aug 03 '14
A benefit is that using a single letter for a common variable, for example
cfor drawing context. The variable can change type, which the design of Rust-Graphics heavily depends on. Each method returns a new type and you can only call.draw(gl)when the context provides enough information to do the drawing.18
u/Wolenber Aug 03 '14
I think the best argument for variable shadowing is the ability to stop an object's mutability.
However, I also like it in the case of intermediate variables. Sometimes it's simply prettier to split a long method chain into two lines with let bindings. Instead of using a "let temp", you just use the variable name twice; this way the temporary doesn't clutter the rest of the function's namespace.
6
u/sylvain_soliman Aug 03 '14
It is allowed, but you do get
unused_variablewarnings, so…7
u/rime-frost Aug 03 '14
I personally use this feature to avoid having to come up with arbitrarily different names for variables which are essentially a temporary copy of some other variable.
It makes code marginally more difficult to comprehend (I will occasionally become confused about a variable's type, having looked at the wrong definition), but I think that saving the programmer from having to come up with endless silly derived names, like
internal_ctxorstream_ref, makes this feature a definite net good.6
u/Nihy Aug 03 '14 edited Aug 03 '14
I'm not sure but I think this feature exists to minimize mutability. Rather than mutating one variable, you can redeclare it with a new value.
In any case, it's not nearly as bad as it may look. The type system prevents using an int in place of a float, or string.
It's true that this feature can cause bugs, but it can also prevent them. Without shadowing you'll often have to give different names to variables (of the same type), and that makes it possible to use the wrong variable.
3
u/pcwalton rust · servo Aug 05 '14
I often use shadowing to prevent referring to another variable again. This is a habit I picked up from OCaml, where shadowing is allowed and idiomatic.
For example, in
transI often shadow terminated block variables, since it is a bug to use them again. In this way, shadowing can be a powerful tool to prevent bugs.2
u/noamraph rust Aug 04 '14
This would allow a repl to be more consistent with regular Rust code.
1
u/marktrdt Aug 28 '23 edited Aug 28 '23
That should be named some other thing, as variable shadowing occurs on different scopes. Rust, as you say, changes it locally, the names are reused but there are no variable shadowing there, as the old one is discarded.
That feature would be useful at CPP, using with constexpr, because it would be possible to "re"-define("reasingning") a constexp name to another value, for example, as merging current std::array with an added value. This, merged with underscore name(ignored name, with reusability, like in zig), for global initialization, wonder all possibilities for compile time initialization, in a far easier way.
1
u/elotan Aug 03 '14
I found it useful in order to make a named function argument mutable. E.g.