Common Patterns to Satisfy the Borrow Checker

Satisfy the Rust borrow checker by using immutable references for reading or a single mutable reference for writing, ensuring no conflicting borrows exist simultaneously.

Use references to borrow data without taking ownership, or clone the data if you need independent copies. The borrow checker enforces rules: you can have either one mutable reference or any number of immutable references, but not both at the same time.

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