对unwrapped属性的引用失败:使用部分移动的值:`self`

时间:2016-11-15 16:33:32

标签: rust structure

我尝试将一个解包的字符串引用发送到为结构实现的静态方法。这是一个简化的代码:

fn main() {
    let a = A {p: Some("p".to_string())};
    a.a();
}

struct A {
    p: Option<String>
}

impl A {
    fn a(self) -> Self {
        Self::b(&self.p.unwrap());
        self
    }
    fn b(b: &str) {
        print!("b: {}", b)
    }
}

失败了:

error[E0382]: use of partially moved value: `self`
  --> src/main.rs:14:13
   |
13 |             Self::b(&self.p.unwrap());
   |                      ------ value moved here
14 |             self
   |             ^^^^ value used here after move
   |
   = note: move occurs because `self.p` has type `std::option::Option<std::string::String>`, which does not implement the `Copy` trait

我认为实施Copy特征不是解决方案。 如何解开p并将其作为&str传递给b

我按照Can't borrow File from &mut self (error msg: cannot move out of borrowed content)

中的建议修改了我的代码
fn main() {
    let a = A {p: Some("p".to_string())};
    a.a();
}

struct A {
    p: Option<String>
}

impl A {
    fn a(self) -> Self {
        let c = self.p.as_ref().unwrap();
        Self::b(&c);
        self
    }
    fn b(b: &str) {
        print!("b: {}", b)
    }
}

导致不同的错误:

error[E0505]: cannot move out of `self` because it is borrowed
  --> src/main.rs:15:13
   |
13 |             let c = self.p.as_ref().unwrap();
   |                     ------ borrow of `self.p` occurs here
14 |             Self::b(&c);
15 |             self
   |             ^^^^ move out of `self` occurs here

1 个答案:

答案 0 :(得分:4)

正如Can't borrow File from &mut self (error msg: cannot move out of borrowed content)中所述,您无法在借用的值上调用unwrap,因为unwrap取得了该值的所有权。

更改为as_ref借用值self。您不允许移动值(包括返回该值),而对它的任何引用都是未完成的。这意味着您需要在需要移动值之前将借用的生命周期限制为结束:

fn a(self) -> Self {
    {
        let c = self.p.as_ref().unwrap();
        Self::b(c);
    }
    self
}

它可能是您示例的工件,但代码非常奇怪。我把它写成

impl A {
    fn a(self) -> Self {
        self.b();
        self
    }

    fn b(&self) {
        print!("b: {}", self.p.as_ref().unwrap())
    }
}

impl A {
    fn a(&self) {
        print!("a: {}", self.p.as_ref().unwrap())
    }
}