• 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 2

Lesson 2 of 61

Borrowing Basics

Lesson Progress: 0%

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

Borrowing means passing a value by reference instead of transferring ownership. When a function takes &Tas a parameter, it borrows the value — it can read it but does not own it. After the function returns, the original variable is still valid because ownership was never moved. Borrowing avoids unnecessary cloning and allows functions to work with data without taking it over. This lesson shows how to write functions that borrow their arguments.

Code Example
fn show(val: &i32) {
    println!("{}", val);
}
fn main() {
    let x = 5;
    show(&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
  • fn show(val: &i32) takes a reference to an i32.
  • The function borrows x when called with show(&x).
  • Inside the function, val is a reference that can be read.
  • After show returns, x is still valid.
  • The output is 5.
Code Example
fn show(msg: &String) {
    println!("{}", msg);
}
fn main() {
    let s = String::from("hi");
    show(&s);
    println!("{}", s);
}

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
  • fn show(msg: &String) borrows a String reference.
  • After calling show(&s), s is still usable because ownership was not moved.
  • Without borrowing, passing s directly would move ownership and make s invalid.
  • Both calls print hi.
  • Borrowing preserves the original variable's validity.
Code Example
fn add(a: &i32, b: &i32) -> i32 {
    *a + *b
}
fn main() {
    let x = 3;
    let y = 7;
    println!("{}", add(&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
  • fn add(a: &i32, b: &i32) -> i32 borrows two values and returns a new one.
  • *a + *b dereferences both references to compute the sum.
  • The original x and y are not moved.
  • The returned i32 is owned by the caller.
  • The output is 10.
Code Example
fn length(s: &String) -> usize {
    s.len()
}
fn main() {
    let word = String::from("Rust");
    println!("{}", length(&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
  • s.len() works directly on a &String because method calls auto-dereference.
  • The function borrows word, so word remains valid after the call.
  • length can read the string but cannot modify it.
  • The return type is usize, the length of the string.
  • The output is 4.
Code Example
fn first(s: &String) -> &str {
    &s[0..1]
}
fn main() {
    let word = String::from("Hi");
    println!("{}", 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
  • Borrowing can return a slice (&str) of the borrowed data.
  • &s[0..1] creates a string slice of the first character.
  • The returned slice is valid as long as the borrowed String is alive.
  • The function only reads the string, it does not modify it.
  • The output is H.
Code Example
fn print_both(a: &i32, b: &i32) {
    println!("{}", a);
    println!("{}", b);
}
fn main() {
    let x = 1;
    let y = 2;
    print_both(&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 at once.
  • Both a and b are references passed to print_both.
  • The function reads them but does not take ownership.
  • After the call, x and y are still valid.
  • The output is 1 and 2 on separate lines.

Test Incomplete

What does it mean for a function to borrow a value?

Question #

1/15

Score

0/0 - 0.0 %