Why do I need rebinding/shadowing when I can have mutable variable binding? Consider:
let x = a();
let x = b(x);
vs.
let mut x = a();
x = b(x);
Mutable variable binding allows a mutable borrow of that variable over this. But does shadowing have some advantages over mutable bindings?
Shadowing allows us to reuse variable names rather than creating unique ones; it allows us to transform variables without making them mut; it lets us convert type without manually creating two vars of different types (makes things concise by automating this); shadowing is optional (you don't have to do it) and not ...
variable binding (plural variable bindings) (programming) the association between a variable name (identifier) and its value.
Because the two have totally different effects.
To really understand what is going on, we need to start at the beginning: what is a binding? What does binding mean?
Let's consider a simple function: fn hello() -> String;
.
When invoking this function like so:
fn main() {
hello();
}
What happens?
The function returns a String
, which is promptly discarded (executing Drop
as it is thereby freeing its memory).
The result is dropped because it was not bound to a variable name, and the rules of the language say that if not bound then it can be promptly dropped1.
If we bind this result, however, we prolong the life of this value, and we can access it via this binding... for a while.
fn main() {
let value = hello();
std::mem::drop(value);
println!("{}", value); // Error: moved out of value
}
This is the issue at hand: in Rust, the lifetime of a value is independent from the scope of a binding.
A value need not even be dropped before its binding exits its scope: it can be transferred to another (similar to returning from a function).
fn main() {
let x;
{
let y = hello();
x = y;
}
println!("{}", x);
}
1the same happens if binding to _
.
So, now we armed with the fact that bindings and values differ, let's examine the two snippets.
A first shadowing snippet, differing from yours:
fn main() {
let x = a();
let x = b();
}
Steps, in order:
a()
creates a value, which is bound to x
b()
creates a value, which is bound to x
b()
is droppeda()
is droppedNote that the fact that x
is re-bound does not affect the lifetime of the value that was previously bound.
Technically, it behaves exactly as if the result of b()
was bound to y
, with the sole exception that the previous x
binding is not accessible while y
is in scope.
Now, the mutable snippet:
fn main() {
let mut x = a();
x = b();
}
Steps, in order:
a()
creates a value, which is bound to x
b()
creates a value, which is bound to x
, and the previous value (created by a()
) is droppedb()
is droppedOnce again, accessing the previous value is impossible, however whilst with shadowing it's impossible temporarily (if shadowing in a smaller scope), with assignment it's impossible forever since the value is dropped.
One answer I found myself: shadowing can change variable type.
let x = get_some_string();
let x = x.smart_parse_int();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With