🦀 Rust Beginner

What is borrowing in Rust?

Answer

Borrowing allows you to use a value without taking ownership of it, by creating a reference. An immutable reference &T allows read-only access and multiple immutable borrows can exist simultaneously. A mutable reference &mut T allows read-write access, but only one mutable reference can exist at a time and no immutable references can coexist with it — this prevents data races at compile time. References must always point to valid data (the borrow checker enforces this via lifetimes). Borrowing is how Rust lets you pass data to functions without moving ownership, so the caller retains ownership after the function returns.