While checking out tutorials about Rust, I came across a problem with the borrow checker. The following code doesn't compile:
struct Car {
model: String,
}
struct Person<'a> {
car: Option<&'a Car>,
}
impl<'a> Person<'a> {
fn new() -> Person<'a> {
Person { car: None }
}
fn buy_car(&mut self, c: &'a Car) {
// how to say that Person don't borrow the old car any longer?
self.car = Some(c);
}
}
fn main() {
let civic = Car { model: "Honda Civic".to_string() };
let mut ghibli = Car { model: "Maserati Ghibli".to_string() };
let mut bob = Person::new();
bob.buy_car(&ghibli);
bob.buy_car(&civic);
// error: cannot borrow `ghibli` as mutable because it is also borrowed as immutable
let anything = &mut ghibli;
}
I understand that because of it's lexical nature Rust's borrow checker can't recognize that the borrow of ghibli
has already ended.
But I would really like to know how to solve this problem the Rust way? Do I have to use Rc<T>
or Box<T>
in some way?