How to work around the borrow checker

Fix borrow checker errors by ensuring only one mutable reference or multiple immutable references exist at a time, or by cloning data to create independent copies.

You cannot 'work around' the borrow checker; you must restructure your code to satisfy its rules by ensuring only one mutable reference or multiple immutable references exist at a time. Use clone() to duplicate data if you need multiple owners, or split the scope of references to prevent them from overlapping.

fn main() {
    let mut s = String::from("hello");
    let r1 = &s; // Immutable borrow
    println!("r1: {}", r1);
    // r1 is no longer used here
    let r2 = &mut s; // Mutable borrow allowed now
    r2.push_str(", world");
    println!("s: {}", s);
}