rust - Understanding lifetime management while modeling containment relationship -


i'm trying wrap head around rust objects lifetime. performing relationship modeling exercise ran following error.

error: cannot borrow `bob` mutable because `bob.gender` borrowed immutable [e0502] 

the code here:

// business case: // design person type.  person may own car.  person should able buy , sell cars. // 2 persons should able exchange (or trade) cars. // // purpose of exercise: // understand lifetime management in rust while modeling containment relationship. // (meaning: when object contains reference 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 {:?} name {:?} has purchased 2006 {:?}.",              bob.gender,              bob.name,              bob.buy_car(&pickup)); } 

can chime in on missing here? i'm not sure if reference count or box way go, need bit more insight.

your issue boils down using buy_car (which returns nothing) meant use bob.car. that.. you'll want have fmt::debug implemented car struct well. here working fix you.. note of // <----- parts added (here on playground):

#[derive(debug)] // <------------ have compiler implement fmt::debug 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 car separately      println!("a {:?} name {:?} has purchased 2006 {:?}.",              bob.gender,              bob.name,              bob.car); // <------------ pass car debug string } 

as aside - in using string appropriate reduce need pass references , lifetimes around. maybe not important in small example code gets bigger can become tricky.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -