gpt4 book ai didi

rust - 为什么结构解构后会出现 drop call?

转载 作者:行者123 更新时间:2023-11-29 08:03:35 25 4
gpt4 key购买 nike

我正在包装一个 C API。为了简化这个问题,我使用 NonNull::dangling 代替。

use std::ptr::NonNull;

struct Foo(NonNull<i32>);

impl Drop for Foo {
fn drop(&mut self) {
println!("Foo::drop: {:?}", self.0);
}
}

struct Moo(NonNull<i32>);
//impl Drop for Moo exists, but not important for question

fn f1() -> Result<Foo, String> {
Ok(Foo(unsafe { NonNull::dangling() }))
}

fn f2() -> Result<Moo, String> {
f1().map(|Foo(x)| Moo(x))//1
}

fn main() {
f2();
}

在第 (1) 点,我解包/解构 Foo。我希望在那之后,不应该调用 Foo::drop,但是由于某种原因打印了 Foo::drop

解构 (let Struct1 { field1, field2, .. } = struct1;) 应该阻止调用 Struct1::drop 我错了吗?

最佳答案

如果您将 NonNull 替换为未实现 Copy 的结构, 行为更清晰:

#[derive(Debug)]
struct NoCopy;

struct Foo(NoCopy);

impl Drop for Foo {
fn drop(&mut self) {
println!("Foo::drop: {:?}", self.0);
}
}

struct Moo(NoCopy);
//impl Drop for Moo exists, but not important for question

fn f1() -> Result<Foo, String> {
Ok(Foo(NoCopy))
}

fn f2() -> Result<Moo, String> {
f1().map(|Foo(x)| Moo(x))//1
}

fn main() {
f2();
}

这会导致此错误:

error[E0509]: cannot move out of type `Foo`, which implements the `Drop` trait
--> src/main.rs:20:15
|
20 | f1().map(|Foo(x)| Moo(x))//1
| ^^^^-^
| | |
| | data moved here
| cannot move out of here
|
note: move occurs because `x` has type `NoCopy`, which does not implement the `Copy` trait
--> src/main.rs:20:19
|
20 | f1().map(|Foo(x)| Moo(x))//1
|

因此,在 (1) 中,您从 Foo 中复制了 NonNull 并且 Foo 被丢弃。

关于rust - 为什么结构解构后会出现 drop call?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55582127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com