Search⌘ K

Exercises

Explore practical exercises focused on Rust references, borrowing rules, lifetimes, and mutability to deepen your understanding. Learn to identify and fix common bugs, manage mutable and immutable references, effectively use dereferencing, and handle borrow errors. These tasks will help solidify your skills in Rust's core ownership principles and safe memory management.

Exercise 1

The is_five function below has a bug. You can fix it by either adding in a borrow (&) or deref (*). Try fixing it both ways.

Rust 1.40.0
fn is_five(x: &i32) -> bool {
x == 5
}
fn main() {
assert!(is_five(&5));
assert!(!is_five(&6));
println!("Success!");
}

Exercise 2

Fix the call to the add function inside ...