• Home
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • React
      • Introduction to React
      • React Developer
    • Python
      • Introduction to Python
      • Python Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Reading Grounds

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds
  • Featured on DanielLaunches

Courses

  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

Rust Developer - Lesson 3

Lesson 3 of 61

Immutable Borrowing

Lesson Progress: 0%

Lesson Progress: 0%
Immutable Borrowing
Lesson Incomplete
← Previous: Borrowing Basics
Lesson 3 of 61
Next: Mutable References →

An immutable (shared) reference &T allows reading a value but not modifying it. Many immutable references to the same data can exist at the same time. This makes immutable borrowing safe for concurrent reading. If you try to mutate through an immutable reference, the compiler will reject it. This lesson explores creating and using multiple immutable references.

Code Example
fn main() {
    let x = 5;
    let r1 = &x;
    let r2 = &x;
    println!("{} {}", r1, r2);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Two immutable references r1 and r2 are created to the same x.
  • Both references can read the value simultaneously.
  • This is safe because neither reference can change the data.
  • The output is 5 5.
  • Shared references are sometimes called “borrowed” references.
Code Example
fn read(a: &i32) {
    println!("{}", a);
}
fn main() {
    let x = 10;
    read(&x);
    read(&x);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A function with a &i32 parameter can be called multiple times on the same value.
  • Each call creates a new immutable borrow.
  • The function reads x without modifying it.
  • The output is 10 printed twice.
  • Immutable borrowing is the default and most common way to pass data to functions.
Code Example
fn read(a: &i32, b: &i32) {
    println!("{}", a + b);
}
fn main() {
    let x = 3;
    let y = 4;
    read(&x, &y);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A function can borrow multiple values with separate immutable references.
  • Both &x and &y are passed at the same time.
  • Inside the function, a + b dereferences both references.
  • The output is 7.
  • Multiple immutable borrows from different variables are always allowed.
Code Example
fn print_len(s: &String) {
    println!("{}", s.len());
}
fn main() {
    let word = String::from("hello");
    print_len(&word);
    print_len(&word);
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A String can be immutably borrowed multiple times.
  • print_len reads the length without modifying the string.
  • Both calls borrow word simultaneously.
  • The output is 5 twice.
  • The original word remains the owner after both calls.
Code Example
fn get_first(s: &String) -> &str {
    &s[0..1]
}
fn main() {
    let word = String::from("abc");
    println!("{}", get_first(&word));
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Immutable references can return slices of the borrowed data.
  • get_first borrows the string and returns a slice reference.
  • The slice &s[0..1] is valid as long as the original String exists.
  • The output is a.
  • Returning a reference from a borrowed parameter is common in Rust.
Code Example
fn add_three(a: &i32, b: &i32, c: &i32) -> i32 {
    *a + *b + *c
}
fn main() {
    let x = 1;
    let y = 2;
    let z = 3;
    println!("{}", add_three(&x, &y, &z));
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Rust Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A function can borrow three values immutably at once.
  • *a + *b + *c dereferences each reference to compute the sum.
  • All three references are read-only and coexist safely.
  • The output is 6.
  • There is no limit to how many immutable references you can create.

Test Incomplete

How many immutable references can exist to the same data at once?

Question #

1/15

Score

0/0 - 0.0 %