无法在 self.attribute 移出 Box::into_raw 的借用内容

Cannot move out of borrowed content for Box::into_raw on self.attribute

pub struct Themepark {
    attraction: Box<Attraction>
}

注意Attraction是特质!

impl Themepark {

    pub fn open(&mut self) -> Result<(), ()> {

        let attraction = Box::into_raw(self.attraction);

    ...
    }

}

这给了我

> cannot move out of borrowed content

for self.attraction inside Box::into_raw

现在我明白那个特定的 错误消息 是什么意思,但我不明白如何解决它,因为 Box::into_raw 期望 Box<T>作为参数,而不是参考或任何东西。

https://doc.rust-lang.org/std/boxed/struct.Box.html#method.into_raw

在可变借用 self 时,您将无法在 self.attraction 上使用该函数;根据其文档的第一行:

Consumes the Box

您要么需要 .clone() 它,要么使用消耗 self 的函数(例如 fn open(self))。

我建议重新阅读 The Rust Book's chapter on Ownership