What Is Mutable Borrowing in Rust?

Mutable borrowing in Rust allows modifying data through a reference using &mut, enforcing exclusive access to prevent data races.

Mutable borrowing in Rust is the act of creating a reference to a value that allows you to modify it, using the &mut syntax. You can only have one mutable reference to a particular piece of data in a particular scope at a time, and you cannot have a mutable reference while an immutable reference exists. This rule prevents data races and ensures memory safety at compile time.

fn main() {
    let mut number = 5;
    let mutable_ref = &mut number;
    *mutable_ref = 6; // Modifies the original value
    println!("{number}"); // Prints 6
}