Coding/Learn/rust/references and borrowing
Rust Foundational Course
01.Introduction to Rust & Memory Safety
02.Formatting, Comments & Macros (println!)
03.Variables, Mutability & Shadowing
04.Scalar & Compound Types (Integers, Floats, Tuples, Arrays)
05.Functions, Parameters & Return Expressions
06.Conditionals & If as an Expression
07.Ownership Rules & Move Semantics
08.References & Borrowing (&T)
09.Mutable References (&mut) & Aliasing XOR Mutability
10.Slices & String Types (&str vs String)
11.Loops in Rust (loop, while, for)
12.Pattern Matching with match Expressions
13.Structs, Associated Functions & Methods (impl)
14.Enums & the Option<T> Type (No Null!)
15.Concise Pattern Matching (if let & let else)
16.Vectors & Growable Collections (Vec<T>)
17.Error Handling with Result<T, E> & the ? Operator
18.Traits, Shared Behavior & Generics
19.Closures & Functional Iterators (map, filter, collect)
20.Capstone: High-Performance In-Memory Log Telemetry Engine
Course Certificate0%

Complete all lessons to earn

LearnReferences & Borrowing (&T)
8 / 20 0%
Lesson 08 of 20
6 mins read

References & Borrowing (&T)

PreviousNext Lesson
In Plain English

Borrowing lets you create references (&T) to data without taking ownership. Because the referencing function does not own the data, the original variable remains valid and no heap reallocation occurs.

The Mental Model

"Borrowing is like showing someone a photo on your phone: they can look at it (read reference), but you still keep the phone (retain ownership)."

Deep Dive: How It Works

The & Symbol: Indicates a reference that borrows access without moving ownership.

Immutability of References: By default, references (&T) are read-only.

Unlimited Readers: You can have any number of simultaneous immutable references to a value.

Syntax Blueprint

fn calculate_len(s: &String) -> usize { s.len() } // Borrowed, not moved

Passing &s borrows the string. The caller retains ownership after the function returns.

PointCore Rules to Remember

Point star
Borrowing: Access data via & reference without moving ownership.
Point star
No Heap Overhead: References are just pointers under the hood with zero runtime cost.
Point star
Referential Safety: Rust guarantees references always point to valid memory (no dangling pointers).

Common Beginner Traps & How to Fix Them

Mistake:Trying to modify data through an immutable reference.

Why it happens: &T references are strictly read-only.

How to fix: Use a mutable reference (&mut T) if mutation is required.

Pro Tip: Prefer passing &str instead of &String to functions to accept both string literals and heap strings.

Live Interactive Example

Hit Run Code to see it live
Borrowing without Moving
Rust 1.78 / rustc
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: The message variable remains fully accessible after get_length because it was borrowed.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Borrow a string and print length

  • PointWrite fn print_info(title: &String) that prints "Title: {title}".
  • PointIn main(), let book = String::from("Rust Manifesto");
  • PointCall print_info(&book) and then print "Owner still has: Rust Manifesto".
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates