- 📘 Day 5 - Ownership & Borrowing
Welcome to Day 5 of the 30 Days of Rust! 🎉 Today, we dive into Ownership and Borrowing. Understanding these concepts is crucial because they form the core of Rust's memory safety model. Let’s explore them together! 🚀
Congratulations! 🎉 You've taken the first step in your journey to master the 30 Days of Rust programming challenge. In this challenge, you will learn the fundamentals of Rust and how to harness its power to write efficient, fast, and safe code. By the end of this journey, you'll have gained a solid understanding of Rust's core concepts and best practices, helping you become a confident Rustacean. 🦀
Feel free to join the 30 Days of Rust community on Discord, where you can interact with others, ask questions, and share your progress!
In Rust, Ownership and Borrowing:
- Help manage memory automatically and safely.
- Ensure there are no data races or dangling references.
- Define how values are passed around and used in Rust programs.
We will cover:
- The concept of ownership and borrowing.
- How to pass data by value and by reference.
- Managing mutable and immutable references.
- Applying these concepts with examples and exercises.
Ensure that you have your Rust environment set up correctly from Day 1. If you haven’t installed Rust yet, please refer to the setup instructions from Day 1.
In Rust, every value has a single owner. When the owner goes out of scope, the value is automatically dropped (freed from memory).
Example:
fn main() {
let s = String::from("Hello, Rust!");
println!("{}", s); // s is valid here
}
// s goes out of scope and is dropped
When a variable is assigned to another, the ownership is moved, not copied.
Example:
fn main() {
let s1 = String::from("Rust");
let s2 = s1; // s1 is now invalid, ownership is moved to s2
println!("{}", s2);
// println!("{}", s1); // This will cause an error
}
Output:
Rust
Borrowing allows you to pass references to values without taking ownership.
Example:
fn main() {
let s = String::from("Hello");
print_string(&s); // Passing a reference
println!("{}", s); // s is still valid here
}
fn print_string(s: &String) {
println!("{}", s);
}
Output:
Hello
Hello
You can borrow a mutable reference to allow modifying the data without taking ownership.
Example:
fn main() {
let mut s = String::from("Hello");
modify_string(&mut s);
println!("{}", s); // Modified string
}
fn modify_string(s: &mut String) {
s.push_str(", world!");
}
Output:
Hello, world!
- Each value has a single owner.
- When the owner goes out of scope, the value is dropped.
- You can have multiple immutable references, but only one mutable reference at a time.
Passing values to functions can also transfer ownership or borrow values, depending on how they are passed.
Example:
fn main() {
let s = String::from("Rust");
takes_ownership(s); // s is moved, and no longer valid here
let x = 5;
makes_copy(x); // x is still valid because integers are Copy
}
fn takes_ownership(s: String) {
println!("{}", s);
}
fn makes_copy(x: i32) {
println!("{}", x);
}
Output:
Rust
5
Write a program that:
- Demonstrates moving and copying with variables.
- Creates functions that take ownership of their parameters and return a result.
- Uses references to avoid unnecessary data copying.
- Write a function that takes a string, borrows it, and returns its length.
- Create a program that demonstrates moving ownership between variables.
- Implement a function that accepts a mutable reference and modifies the data.
- Write a program that uses mutable references to swap two variables.
- Implement a function to clone a vector without transferring ownership.
- Create a recursive function to find the factorial of a number using borrowed values.
- Write a function that counts the occurrences of a character in a string without taking ownership.
- Rust Ownership Explained
- Borrowing and References in Rust
- Rust: Understanding Ownership and Borrowing
- Learned about the core concepts of ownership and borrowing.
- Explored passing data by value and reference.
- Understood the rules and nuances of working with mutable and immutable references.
🌟 Great job on completing Day 5! Keep practicing, and get ready for Day 6 where we will explore Structs in Rust!
Thank you for joining Day 5 of the 30 Days of Rust challenge! If you found this helpful, don’t forget to star this repository, share it with your friends, and stay tuned for more exciting lessons ahead!
Stay Connected
📧 Email: Hunterdii
🐦 Twitter: @HetPate94938685
🌐 Website: Working On It(Temporary)