在 Rust 的匹配语句中解构 Option<Box<_>>

Destructuring an Option<Box<_>> inside a match statement in Rust

我有一个大物体需要装在另一个物体中,但我不一定一直需要它。所以我想使用 if 语句来获取可选的盒装 TempStructure,但我不确定如何同时解构和取消引用。

示例:

pub struct TempStructure {
    lots_of_data: [u64; 64],
}
pub struct Structure {
    pending_removal: Option<Box<(TempStructure, bool)>>,
}
impl Structure {
    pub fn do_somthing(&mut self) {
        // How do I destructure the Option and dereference the Box to get TempStructure?
        if let Some((temp_structure, some_boolean)) = self.pending_removal.take() { 
            // Do something with temp_structure and some_boolean
        }
    }
}

当我这样做时 ^^^ 我得到一个 expected struct `std::boxed::Box`, found tuple 错误。

匹配后取消引用框:

if let Some(inner) = self.pending_removal.take() {
    let (temp_structure, some_boolean) = *inner;
    // Do something with temp_structure and some_boolean
}

(playground)

如果您认为这有点笨拙,那您是对的。在夜间,您可以使用不稳定的 box_patterns 功能为此启用更好的语法(尽管 this might never be stabilized):

if let Some(box (temp_structure, some_boolean)) = self.pending_removal.take() {
    // Do something with temp_structure and some_boolean
}

(playground)

您可以通过在 take():

之后添加 .as_deref() 来解决这个问题
pub struct TempStructure {
    lots_of_data: [u64; 64],
}
pub struct Structure {
    pending_removal: Option<Box<(TempStructure, bool)>>,
}
impl Structure {
    pub fn do_somthing(&mut self) {
        // How do I destructure the Option and dereference the Box to get TempStructure?
        if let Some((temp_structure, some_boolean)) = self.pending_removal.take().as_deref() { 
            // Do something with temp_structure and some_boolean
        }
    }
}

Box<T> 取消引用 &Tas_deref() 取消引用 Option 的值,因此它给你一个 &T 出你的 Option<Box<T>>.

编辑:另一种选择是取消引用 Box 以将值移出其中:

        if let Some((temp_structure, some_boolean)) = self.pending_removal.take().map(|boxed| *boxed) { 
            // assignments just for demonstration purposes that we now get
            // owned values rather than references.
            let _: TempStructure = temp_structure;
            let _: bool = some_boolean;
        }