Because they have completely different effects.
To understand what is happening, we need to start from the very beginning: what is a binding? What does binding mean?
Consider a simple function: fn hello() -> String;.
When calling this function:
fn main() {
hello();
}
What's happening?
String, ( Drop, ).
, , , , 1.
, , ... .
fn main() {
let value = hello();
std::mem::drop(value);
println!("{}", value);
}
- : Rust .
, : ( ).
fn main() {
let x;
{
let y = hello();
x = y;
}
println!("{}", x);
}
1 , _.
, , , .
, :
fn main() {
let x = a();
let x = b();
}
:
a() , xb() , x- ,
b(), - ,
a(),
, , x , .
, b() y, , x , y - .
:
fn main() {
let mut x = a();
x = b();
}
:
a() , xb() , x, ( a())- ,
b(),
, , , ( ), , .