Structs and circural references



examples/struct/circural-references/src/main.rs
fn main() {
    let mut joe = Person {name: "Joe".to_string(), partner: None};
    #[allow(unused_mut)]
    let mut jane = Person {name: "Jane".to_string(), partner: None};
    dbg!(&joe);
    dbg!(&jane);
    joe.partner = Some(&jane);
    //jane.partner = Some(&joe);
    dbg!(&joe);
    dbg!(&jane);

}

#[derive(Debug)]
#[allow(dead_code)]
struct Person<'a> {
    name: String,
    partner: Option<&'a Person<'a>>,
}



error[E0506]: cannot assign to `jane.partner` because it is borrowed
 --> src/main.rs:8:5
  |
7 |     joe.partner = Some(&jane);
  |                        ----- `jane.partner` is borrowed here
8 |     jane.partner = Some(&joe);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^ `jane.partner` is assigned to here but it was already borrowed
9 |     dbg!(&joe);
  |          ---- borrow later used here

For more information about this error, try `rustc --explain E0506`.
error: could not compile `circural-references` (bin "circural-references") due to previous error