5/5
# Borrowing rules ## Example Execute the following command to run [`./solutions/examples/borrow.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/borrowing_rules/solutions/examples/borrow.rs) ```shell cargo run --example borrow ``` Execute the following command to run [`./solutions/examples/borrow_func.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/borrowing_rules/solutions/examples/borrow_func.rs) ```shell cargo run --example borrow_func ``` ## Exercises Exercises are in [`./exercises/src/main.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/borrowing_rules/exercises/src/main.rs) ### Exercise 1 ```rust let s = String::from("Rust"); let s1 = &mut s; let s2 = &mut s; ``` Modify 2 lines to fix this code. `s1` and `s2` must be references to `s`. ### Exercise 2 ```rust let mut s = String::from("Rust"); let s1 = &mut s; let s2 = &mut s; ``` Comment out a single line to fix the code. `s1` must be a reference. ### Exercise 3 ```rust let s = String::from("Rust"); print_len(s); ``` Modify the function definition of `print_len` so that the code compiles. ## Test ```shell cargo test ```
Execute the following command to run ./solutions/examples/borrow.rs
Execute the following command to run ./solutions/examples/borrow_func.rs
Exercises are in ./exercises/src/main.rs
Modify 2 lines to fix this code. s1
and s2
must be references to s
.
Comment out a single line to fix the code. s1
must be a reference.
Modify the function definition of print_len
so that the code compiles.
A core primer on Rust's Borrowing Rules - Understand the fundamental principles that allow Rust to guarantee memory safety by managing data access, preventing data races and dangling references. Solidify your knowledge by tackling exercises on mutable reference conflicts and adapting function signatures for proper reference handling.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on August 15, 2025
Rust Developer
Rust Programming BasicsDuration: 6min
Duration: 18min
Duration: 47min
Duration: 15min
Duration: 19min
Duration: 8min
Duration: 12min
Duration: 46min
Duration: 14min
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on August 15, 2025