📖 Scenario: Imagine you have a book that you want to share with your friends. Instead of giving the book away, you let them read it while you still keep it. This way, the book is safe, and everyone can enjoy it without losing it.In Rust programming, borrowing works like sharing the book. It helps you use data without taking ownership, so the data stays safe and no mistakes happen.
🎯 Goal: You will create a simple Rust program that shows why borrowing is needed. You will first create a string, then borrow it to a function to read it without taking ownership. Finally, you will print the original string to prove it is still usable.
📋 What You'll Learn
Create a String variable with exact text
Create a function that takes a borrowed string slice
Call the function with a borrowed reference
Print the original String after borrowing
💡 Why This Matters
🌍 Real World
Borrowing is like lending items to friends without giving them away. It helps programs share data safely without mistakes.
💼 Career
Understanding borrowing is essential for writing safe and efficient Rust code, a skill valued in systems programming and software development jobs.
Progress0 / 4 steps
1
Create a String variable
Create a variable called book and set it to a String with the exact text "Rust Programming Book".
Rust
Hint
Use String::from to create a String from text.
2
Create a function that borrows the string
Create a function called read_book that takes a borrowed string slice parameter named book_ref of type &str. Inside the function, print the text "Reading: {book_ref}" using println!.
Rust
Hint
Function parameter should be &str to borrow the string.
3
Call the function with a borrowed reference
Inside the main function, call the read_book function and pass a borrowed reference of book using &book.
Rust
Hint
Use &book to borrow the string when calling the function.
4
Print the original String after borrowing
After calling read_book inside main, print the original book variable using println! with the exact text "Original book: {book}".
Rust
Hint
Printing the original book after borrowing shows it is still usable.
Practice
(1/5)
1. Why does Rust require borrowing when accessing data instead of always transferring ownership?
easy
A. To allow multiple parts of code to read data without copying it
B. To make the code run slower for safety
C. To force programmers to write more code
D. To prevent any data from being changed
Solution
Step 1: Understand ownership and borrowing
Ownership means one variable controls the data. Borrowing lets others use data without owning it.
Step 2: Why borrowing is useful
Borrowing allows multiple parts of code to read data safely without making copies or moving ownership.
Final Answer:
To allow multiple parts of code to read data without copying it -> Option A
Quick Check:
Borrowing = safe shared access [OK]
Hint: Borrowing means sharing data without moving ownership [OK]
Common Mistakes:
Thinking borrowing slows down code
Believing borrowing prevents all data changes
Confusing borrowing with copying data
2. Which of the following is the correct syntax to borrow a variable immutably in Rust?
easy
A. let x = ref value;
B. let x = &value;
C. let &x = value;
D. let mut x = &mut value;
Solution
Step 1: Identify borrowing syntax
In Rust, borrowing immutably uses the ampersand before the variable: &value.
Step 2: Match correct syntax
The correct way to bind a reference is let x = &value;. Other options are invalid or mean something else.
Final Answer:
let x = &value; -> Option B
Quick Check:
Immutable borrow = &value [OK]
Hint: Use & before variable to borrow immutably [OK]
Common Mistakes:
Using let x = ref value; invalid syntax
Confusing &x with &value
Using mutable borrow (&mut) for immutable access
3. What will be the output of this Rust code?
fn main() {
let s = String::from("hello");
let r1 = &s;
let r2 = &s;
println!("{} and {}", r1, r2);
}
medium
A. hello and hello
B. Compilation error due to multiple mutable borrows
C. hello and error
D. Runtime error
Solution
Step 1: Analyze borrowing in code
Two immutable references r1 and r2 borrow s simultaneously, which is allowed.
Step 2: Check output of println!
Both references print the string "hello", so output is "hello and hello".
Final Answer:
hello and hello -> Option A
Quick Check:
Multiple immutable borrows print values [OK]
Hint: Multiple immutable borrows allowed, prints values [OK]
Common Mistakes:
Thinking multiple immutable borrows cause errors
Confusing mutable and immutable borrow rules
Expecting runtime errors for borrowing
4. Identify the error in this Rust code and why borrowing is required:
fn main() {
let mut s = String::from("hello");
let r1 = &s;
let r2 = &mut s;
println!("{} and {}", r1, r2);
}
medium
A. No error, code runs fine
B. Cannot borrow `s` as immutable because it is mutable
C. Cannot borrow `s` as mutable because it is already borrowed as immutable
D. Cannot print two variables in println!
Solution
Step 1: Check borrowing rules
Rust forbids mutable borrow while immutable borrow exists to prevent data races.