Understanding Rust Ownership Rules: Clone, Move, Immutable and Mutable Borrow
This article explains Rust's ownership model, detailing how values are owned by variables, released when out of scope, and transferred or accessed through cloning, moving, immutable borrowing, and mutable borrowing, with code examples illustrating each rule and the compiler errors they prevent.
Rust provides memory safety by enforcing ownership rules at compile time, ensuring that code that could cause memory errors is rejected.
The core principles are:
Values are owned by variables.
When a variable goes out of scope, its memory is released.
Values can be used by other variables only according to specific rules.
There are four ways to use a value, each with its own ownership semantics:
Clone
Cloning creates a deep copy of the value; the new variable owns the copy while the original retains ownership. Cloning is costly, so it should be used sparingly.
Move
Moving transfers ownership to a new variable, rendering the original variable unusable. Attempting to use the moved variable results in a compile‑time error.
fn main() {
let mut original_owner = format!("Hello world");
// move occurs to new owner
let new_owner = original_owner;
// attempt to use original_owner will cause a compile‑time error
println!("{}", original_owner);
}Immutable Borrow
An immutable borrow allows read‑only access to a value without transferring ownership. Multiple immutable borrows can coexist, but the original owner cannot be mutated while any immutable borrow is active.
fn main() {
let mut original_owner = format!("Hello world");
// immutable borrow occurs
let borrow_owner = &original_owner;
println!("{}", original_owner);
println!("{}", borrow_owner);
// error: cannot mutate through an immutable borrow
borrow_owner.push('.');
}Mutable Borrow
A mutable borrow gives exclusive read‑write access to a value. While a mutable borrow is active, the original variable cannot be accessed, and only one mutable borrow may exist at a time.
fn main() {
let mut original_owner = format!("Hello world");
// mutable borrow occurs
let borrow_mutable_owner = &mut original_owner;
// compile error if a second mutable borrow is created
let illegal_borrow = &mut original_owner;
println!("{}", borrow_mutable_owner);
}When the mutable borrow goes out of scope, the original variable can be accessed again for reading or writing.
Summary
Clone creates a duplicate; use it only when necessary due to its cost.
Move transfers ownership, making the source variable unusable.
Immutable borrows allow unlimited read‑only access but prevent mutation of the owner while active.
Mutable borrows provide exclusive read‑write access; only one may exist at a time.
Understanding these rules helps write safe, race‑free Rust code.
High Availability Architecture
Official account for High Availability Architecture.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.