2016-08-24 106 views
0

我想围绕锈对象生命周期来包装我的头。当我进行关系建模练习时,我遇到了以下错误。理解生命周期管理,同时建模包含关系

error: cannot borrow `bob` as mutable because `bob.gender` is also borrowed as immutable [E0502] 

的代码是在这里:

// Business Case: 
// Design a person type. A person may own a Car. A person should be able to buy and sell cars. 
// Two persons should be able to exchange (or trade) their cars. 
// 
// Purpose of exercise: 
// Understand lifetime management in Rust while modeling containment relationship. 
// (meaning: when an object contains a reference to another object.) 

struct Car { 
    make: &'static str, 
    model: &'static str, 
    year: &'static str, 
} 

struct Person<'a> { 
    name: &'static str, 
    gender: &'static str, 
    car: Option<&'a Car>, 
} 

impl<'a> Person<'a> { 
    fn new(name: &'static str, gender: &'static str, car: Option<&'a Car>) -> Person<'a> { 
     Person { 
      name: name, 
      gender: gender, 
      car: None, 
     } 
    } 

    fn buy_car(&mut self, c: &'a Car) { 
     self.car = Some(c); 
    } 

    fn sell_car(&mut self) { 
     self.car = None; 
    } 
} 

fn main() { 
    let pickup = Car { 
     make: "Ford", 
     model: "F250", 
     year: "2006", 
    }; 

    let mut bob = Person::new("Bob", "Male", None); 

    println!("A {:?} whose name is {:?} has just purchased a 2006 {:?}.", 
      bob.gender, 
      bob.name, 
      bob.buy_car(&pickup)); 
} 

任何人都可以附和上究竟我在这里失踪?我并不确定引用次数或Box是否是要走的路,需要多一点洞察力。

回答

1

您的问题归结您使用buy_car(返回什么),你可能是指使用bob.car来。要做到这一点..你会想要为你的Car结构体实现fmt::Debug。这是一个工作修复了你..注意,所有我加入了// <----- parts的(here it is on the Playground):

#[derive(Debug)] // <------------ Have the compiler implement fmt::Debug for you 
struct Car { 
    make: &'static str, 
    model: &'static str, 
    year: &'static str, 
} 

struct Person<'a> { 
    name: &'static str, 
    gender: &'static str, 
    car: Option<&'a Car>, 
} 

impl<'a> Person<'a> { 
    fn new(name: &'static str, gender: &'static str, car: Option<&'a Car>) -> Person<'a> { 
     Person { 
      name: name, 
      gender: gender, 
      car: None, 
     } 
    } 

    fn buy_car(&mut self, c: &'a Car) { 
     self.car = Some(c); 
    } 

    fn sell_car(&mut self) { 
     self.car = None; 
    } 
} 

fn main() { 
    let pickup = Car { 
     make: "Ford", 
     model: "F250", 
     year: "2006", 
    }; 

    let mut bob = Person::new("Bob", "Male", None); 

    bob.buy_car(&pickup); // <------- Buy the car separately 

    println!("A {:?} whose name is {:?} has just purchased a 2006 {:?}.", 
      bob.gender, 
      bob.name, 
      bob.car); // <------------ Pass the car into the debug string 
} 

顺便说一句 - 我想看看在使用String酌情减少需要通过周围的引用和寿命。在你的小例子中,可能不那么重要,但随着代码越来越大,它们可能变得棘手。

+0

好了,我没能使用FMT ::调试特质,也叫结构方法为我的变量。编译器错误导致我有点遗憾。我认为这可能与一生的规则有关。至于你的其他评论,我只是将静态生存期字符串替换为字符串&'static str? – Alex

+0

@Alex下面是如何为你的类型的属性设置一个'String'的例子,但接受一个'&'静态str','&str'或者'String'作为它的输入:https:// play。 rust-lang.org/?gist=5dc13bf0a53c9ad0ecc0d40e63072ff5&version=stable&backtrace=0。你不会在你的小样本中做这件事......但随着你的代码变得越来越大,你传递引用(可变,不可变等),它可能变得棘手。 –

+0

了解,非常感谢您的反馈意见。我会在我的结尾做出您所推荐的更改。操场真的很有帮助。 – Alex

相关问题