Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Borrowing and References | Rust Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 10 of 50

·rust

Borrowing and References

15 minutes

Course progress20%

Borrowing and References

Borrowing lets you reference values without taking ownership. This is one of Rust's most powerful features, enabling flexible yet safe code.

Immutable References

&T allows you to read a value without ownership:

fn calculate_length(s: &String) -> usize {
    s.()
}  

 () {
      = ::(\
len
// s goes out of scope, but it doesn't own the String, so nothing is dropped
fn
main
let
s
String
from
"hello\"); let len = calculate_length(&s); // Pass reference, not ownership println!(\"The length of '{}' is {}\", s, len); // s is still valid! }

The reference lets the function read the data without taking ownership. After the function returns, the original owner still has the value.

Mutable References

&mut T allows reading AND writing:

fn append_world(s: &mut String) {
    s.push_str(\" world\");
}

fn main() {
    let mut s = String::from(\"hello\");
    append_world(&mut s);  // Pass mutable reference
    println!(\"{}\", s);  // \"hello world\"
}

Mutable references give temporary write permission while the original owner retains ultimate ownership.

The Borrowing Rules

Rust enforces strict borrowing rules at compile time to prevent data races:

Rule 1: Multiple Immutable References

You can have multiple read-only references:

fn main() {
    let s = String::from(\"hello\");
    let r1 = &s;  // First reference
    let r2 = &s;  // Second reference
    let r3 = &s;  // Third reference
    println!(\"r1: {}, r2: {}, r3: {}\", r1, r2, r3);  // All OK
}

Rule 2: Only One Mutable Reference

At any time, you can have ONE mutable reference OR multiple immutable references, but not both:

fn main() {
    let mut s = String::from(\"hello\");
    let r1 = &s;     // Immutable reference
    let r2 = &s;     // Another immutable
    // let m = &mut s;  // ERROR! Can't have mutable while immutable refs exist
    println!(\"r1: {}, r2: {}\", r1, r2);
}

Why This Rule?

This prevents data races. Consider this scenario in other languages:

// Hypothetically without borrowing rules
let mut data = 10;
let ref1 = &data;      // Reference
// ... if another thread modifies data ...
data = 20;
println!(\"ref1: {}\", ref1);  // Undefined behavior!

Rust's rules prevent this at compile time.

Reference Scope

References go out of scope when they're last used, not when declared:

fn main() {
    let mut s = String::from(\"hello\");
    let r1 = &s;
    let r2 = &s;
    println!(\"r1: {}, r2: {}\", r1, r2);  // Last use of r1 and r2
    
    // r1 and r2 are out of scope here
    let m = &mut s;  // OK! No immutable refs in scope
    println!(\"m: {}\", m);
}

Practical Example

fn check_contains(text: &String, word: &str) -> bool {
    text.contains(word)
}

fn make_uppercase(text: &mut String) {
    *text = text.to_uppercase();
}

fn main() {
    let mut message = String::from(\"hello rust\");
    
    // Immutable borrow
    if check_contains(&message, \"rust\") {
        println!(\"Found rust!\");
    }
    
    // Mutable borrow
    make_uppercase(&mut message);
    println!(\"Message: {}\", message);
}

Key Benefits

  1. No copies: References avoid expensive data duplication
  2. Memory safe: Compiler prevents use-after-free
  3. No garbage collector: Deterministic cleanup
  4. Zero-cost: References compile to pointers, no runtime overhead
Make it stickPractice what you've learned1 quiz · 1 practical test · Optional activities from Rust FundamentalsBrowse activities
Previous lessonOwnership FundamentalsContinue to lesson 11SlicesBack to Rust Fundamentals curriculum