Python References & Borrowing (&T)
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.
"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 movedPassing &s borrows the string. The caller retains ownership after the function returns.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
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.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Borrow a string and print length
Write fn print_info(title: &String) that prints "Title: {title}".
In main(), let book = String::from("Rust Manifesto");
Call print_info(&book) and then print "Owner still has: Rust Manifesto".
Finished reading and practicing?
Mark this lesson as completed to update your course progress.