100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Rust Ownership & Borrowing Cheat Sheet

Rust Ownership & Borrowing Cheat Sheet

Covers Rust's core ownership rules, move semantics, borrowing with references, and the borrow checker rules that prevent data races at compile time.

2 PagesBeginnerMar 25, 2026

Ownership Rules

The three foundational rules of Rust's ownership system.

  • Single owner- Each value has exactly one owner variable at a time
  • Move on assignment- Assigning a non-Copy value to another variable moves it; the original binding becomes invalid
  • Drop on scope exit- When the owner goes out of scope, Rust automatically calls drop to free the value
  • Copy types- Simple stack-only types (integers, bool, char, tuples of Copy types) are copied, not moved
  • Clone for deep copy- Call .clone() to explicitly duplicate heap data instead of moving it

Move Semantics

How assignment and function calls transfer ownership.

rust
let s1 = String::from("hello");let s2 = s1; // s1 is moved into s2; s1 is no longer valid// println!("{}", s1); // compile error: value borrowed after movelet s3 = s2.clone(); // deep copy; both s2 and s3 remain validprintln!("{} {}", s2, s3);fn takes_ownership(s: String) {    println!("{}", s);} // s is dropped herelet s4 = String::from("world");takes_ownership(s4); // s4 moved into the function// s4 is no longer valid here

Immutable Borrowing

Passing references instead of transferring ownership.

rust
fn calculate_length(s: &String) -> usize {    s.len()} // s goes out of scope, but nothing is dropped since it's a referencelet s1 = String::from("hello");let len = calculate_length(&s1); // borrow s1 immutablyprintln!("The length of '{}' is {}.", s1, len); // s1 still valid

Mutable Borrowing

The exclusivity rule enforced by the borrow checker.

rust
fn change(s: &mut String) {    s.push_str(", world");}let mut s = String::from("hello");change(&mut s);println!("{}", s); // "hello, world"// Rule: at any time, either ONE mutable reference// OR any number of immutable references -- never both.let r1 = &s;let r2 = &s;      // OK: multiple immutable borrowsprintln!("{} {}", r1, r2);let r3 = &mut s;  // OK now: r1, r2 are no longer used (NLL)r3.push('!');

Common Borrow-Checker Errors

Errors you'll hit while learning ownership, and what they mean.

  • value borrowed after move- Trying to use a variable after its value was moved elsewhere
  • cannot borrow as mutable more than once- Two &mut references to the same value exist at once
  • cannot borrow as mutable because also borrowed as immutable- Mixing &mut with an active &
  • Dangling reference- Returning a reference to a value that goes out of scope; caught at compile time
  • does not live long enough- A borrowed value's owner is dropped while the reference is still in use
Pro Tip

Non-Lexical Lifetimes (NLL) mean a borrow's scope ends at its last use, not at the end of the block — so you can often reuse a variable mutably right after its last immutable read without restructuring code.

Was this cheat sheet helpful?

Explore Topics

#RustOwnershipBorrowing#RustOwnershipBorrowingCheatSheet#Programming#Beginner#OwnershipRules#MoveSemantics#ImmutableBorrowing#MutableBorrowing#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet