Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 在重新分配可变变量时是否调用析构函数?_Rust - Fatal编程技术网

Rust 在重新分配可变变量时是否调用析构函数?

Rust 在重新分配可变变量时是否调用析构函数?,rust,Rust,假设我定义了一个可变变量,它可能是包含向量或其他动态分配数据的复杂结构,具有Drop特性。在重新分配该变量时,是否在重新分配后立即调用析构函数 设mut x=some_complex_struct; 而有些情况{ // ... x=新的复杂结构; // ... } 我的解释是,x在new\u complex\u struct上获得所有权,它以前拥有的值变为无主,因此它的析构函数将在重新分配后立即调用。我的解释正确吗 我的解释是,x在new\u complex\u struct上获得所有权,它以

假设我定义了一个可变变量,它可能是包含向量或其他动态分配数据的复杂结构,具有
Drop
特性。在重新分配该变量时,是否在重新分配后立即调用析构函数

设mut x=some_complex_struct;
而有些情况{
// ...
x=新的复杂结构;
// ...
}
我的解释是,
x
new\u complex\u struct
上获得所有权,它以前拥有的值变为无主,因此它的析构函数将在重新分配后立即调用。我的解释正确吗

我的解释是,
x
new\u complex\u struct
上获得所有权,它以前拥有的值变为无主,因此它的析构函数将在重新分配后立即调用。我的解释正确吗

structfoo;
为Foo的impl Drop{
fn下降(&mut自我){
println!(“Foo::drop”);
}
}
fn main(){
设mut f=Foo;
因为我在0..5{
println!({}之前,i);
f=Foo;
println!({}之后,i);
}
}
将按预期打印:

Before 0
Foo::drop
After 0
Before 1
Foo::drop
After 1
Before 2
Foo::drop
After 2
Before 3
Foo::drop
After 3
Before 4
Foo::drop
After 4
Foo::drop
铁锈中的破坏者是确定性的,所以这种行为是一成不变的。