• 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 1 of 61

Lesson Progress: 0%

Lesson Progress: 0%
Lesson Incomplete
Lesson 1 of 61
Next: Borrowing Basics →

A reference in Rust lets you point to a value without taking ownership of it. You create an immutable reference with the & operator and access the referenced value with the dereference operator *. The original variable still owns its data and remains valid. You can create multiple references to the same value. Dereferencing happens automatically in many cases, such as inside println!. This lesson covers the basics of creating and using references.

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

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
  • let x = 5; binds the value 5 to x.
  • let r = &x; creates a reference r that points to x without taking ownership.
  • Passing r to println! automatically dereferences and prints 5.
  • The variable x still owns the value and can be used later.
  • References are non-owning pointers — they borrow the value.
Code Example
fn main() {
    let x = 10;
    let r = &x;
    println!("{}", *r);
}

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
  • The *r syntax explicitly dereferences the reference to get the value.
  • println! can also print the reference directly because it knows how to display integers.
  • Dereferencing gives you access to the underlying i32 value.
  • The output is 10.
  • Using * is called the dereference operator.
Code Example
fn main() {
    let x = 42;
    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
  • You can create multiple references to the same value.
  • Both r1 and r2 are &i32 references pointing to x.
  • All references coexist peacefully as long as they are read-only.
  • The output is 42 42.
  • Creating a reference does not move or copy the value.
Code Example
fn main() {
    let s = String::from("hi");
    let r = &s;
    println!("{}", r);
}

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
  • You can also create references to heap-allocated types like String.
  • let r = &s; borrows the String without moving it.
  • The original s still owns the string data after the reference is created.
  • The output is hi.
  • References to String have type &String.
Code Example
fn main() {
    let x = 7;
    let r = &x;
    println!("{}", x);
    println!("{}", r);
}

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
  • The original variable remains usable after creating a reference to it.
  • x is still valid and can be printed directly.
  • r is a reference and prints the same value.
  • Both x and r produce the output 7.
  • This shows that references are non-owning — no ownership transfer occurs.
Code Example
fn main() {
    let x = 3;
    let r = &x;
    let y = *r + 1;
    println!("{}", 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
  • You can dereference a reference with * to use the value in expressions.
  • *r + 1 dereferences r to get 3, then adds 1.
  • The result 4 is stored in y.
  • The original x remains unchanged.
  • Dereferencing does not copy ownership; it only reads the value.

Test Incomplete

What does the & operator do in Rust?

Question #

1/15

Score

0/0 - 0.0 %