语义上,它是同一个对象,只是在您的情况下更改了名称。
但在当前实现中,它会将堆栈数据复制到堆栈中的新位置,因此Vec对象的地址将更改(但堆指针保持不变)。
这样做是为了可以使用新对象隐藏旧名称:
let v = make_first_vec();
let mut v = make_second_vec();
// old v still exists and it would be dropped only at end of block.
更好的示例(可以运行):
struct Droppable(i32);
impl Drop for Droppable{
fn drop(&mut self){
println!("Dropping {}", self.0);
}
}
fn main(){
println!("Same object moved begin");
{
let a = Droppable(1);
let old_ref = &a;
println!("Address is {}", &a as *const _ as usize);
let mut a = a;
println!("Address is {}", &a as *const _ as usize);
let a = a;
println!("Address is {}", &a as *const _ as usize);
}
println!("Same object moved end");
println!("Different object hides begin");
{
let a = Droppable(2);
let old_ref = &a;
println!("Address is {}", &a as *const _ as usize);
let a = Droppable(3);
println!("Address is {}", &a as *const _ as usize);
println!("Old object is still alive and has {}", old_ref.0);
}
println!("Different object hides end");
println!("Different object override begin");
{
let mut a = Droppable(4);
let old_ref = &a;
println!("Address is {}", &a as *const _ as usize);
a = Droppable(5);
println!("Address is same {}", &a as *const _ as usize);
}
println!("Different object override end");
}
它会打印出这个:
Same object moved begin
Address is 140736088967924
Address is 140736088967888
Address is 140736088967892
Dropping 1
Same object moved end
Different object hides begin
Address is 140736088967888
Address is 140736088967892
Old object is still alive and has 2
Dropping 3
Dropping 2
Different object hides end
Different object override begin
Address is 140736088967892
Dropping 4
Address is same 140736088967892
Dropping 5
Different object override end