diff --git a/Cargo.lock b/Cargo.lock index 7854e41..26c2a1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -865,6 +865,10 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "ownership" +version = "0.1.0" + [[package]] name = "parking_lot" version = "0.12.2" diff --git a/challenges/challenges.json b/challenges/challenges.json index 239c7b1..ce1e611 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -167,6 +167,66 @@ "created_at": "2024-06-12T00:00:00Z", "updated_at": "2024-06-12T00:00:00Z" }, + { + "id": 10, + "title": "Basic If-Else Statements", + "slug": "if-else", + "short_description": "Learn how to use if-else statements in Rust to control the flow of your program.", + "language": "RUST", + "difficulty": "EASY", + "track": "CONTROL_FLOW", + "tags": ["control flow", "if-else"], + "created_at": "2024-06-01T00:00:00Z", + "updated_at": "2024-06-01T00:00:00Z" + }, + { + "id": 11, + "title": "Sum of Even Numbers", + "slug": "sum-of-even-numbers", + "short_description": "Implement a function to sum even numbers in a given range using a for loop.", + "language": "RUST", + "difficulty": "EASY", + "track": "CONTROL_FLOW", + "tags": ["for-loop", "iteration", "range"], + "created_at": "2024-06-02T00:00:00Z", + "updated_at": "2024-06-02T00:00:00Z" + }, + { + "id": 12, + "title": "Countdown", + "slug": "countdown", + "short_description": "Implement a countdown timer using a while loop in Rust.", + "language": "RUST", + "difficulty": "EASY", + "track": "CONTROL_FLOW", + "tags": ["while", "loop", "control flow"], + "created_at": "2024-06-02T00:00:00Z", + "updated_at": "2024-06-02T00:00:00Z" + }, + { + "id": 13, + "title": "Weekday from Number", + "slug": "weekday-from-number", + "short_description": "Use Rust's pattern matching to determine the weekday given its number.", + "language": "RUST", + "difficulty": "EASY", + "track": "CONTROL_FLOW", + "tags": ["pattern matching", "match", "control flow"], + "created_at": "2024-06-02T00:00:00Z", + "updated_at": "2024-06-02T00:00:00Z" + }, + { + "id": 34, + "title": "Ownership", + "slug": "ownership", + "short_description": "Understand and implement Rust's ownership model.", + "language": "RUST", + "difficulty": "EASY", + "track": "RUST_BASICS", + "tags": ["ownership", "borrowing", "lifetimes"], + "created_at": "2024-06-13T00:00:00Z", + "updated_at": "2024-06-13T00:00:00Z" + }, { "id": 2, "title": "Character counting string", @@ -251,54 +311,6 @@ "created_at": "2024-05-06T00:00:00Z", "updated_at": "2024-05-06T00:00:00Z" }, - { - "id": 10, - "title": "Basic If-Else Statements", - "slug": "if-else", - "short_description": "Learn how to use if-else statements in Rust to control the flow of your program.", - "language": "RUST", - "difficulty": "EASY", - "track": "CONTROL_FLOW", - "tags": ["control flow", "if-else"], - "created_at": "2024-06-01T00:00:00Z", - "updated_at": "2024-06-01T00:00:00Z" - }, - { - "id": 11, - "title": "Sum of Even Numbers", - "slug": "sum-of-even-numbers", - "short_description": "Implement a function to sum even numbers in a given range using a for loop.", - "language": "RUST", - "difficulty": "EASY", - "track": "CONTROL_FLOW", - "tags": ["for-loop", "iteration", "range"], - "created_at": "2024-06-02T00:00:00Z", - "updated_at": "2024-06-02T00:00:00Z" - }, - { - "id": 12, - "title": "Countdown", - "slug": "countdown", - "short_description": "Implement a countdown timer using a while loop in Rust.", - "language": "RUST", - "difficulty": "EASY", - "track": "CONTROL_FLOW", - "tags": ["while", "loop", "control flow"], - "created_at": "2024-06-02T00:00:00Z", - "updated_at": "2024-06-02T00:00:00Z" - }, - { - "id": 13, - "title": "Weekday from Number", - "slug": "weekday-from-number", - "short_description": "Use Rust's pattern matching to determine the weekday given its number.", - "language": "RUST", - "difficulty": "EASY", - "track": "CONTROL_FLOW", - "tags": [], - "created_at": "2024-06-02T00:00:00Z", - "updated_at": "2024-06-02T00:00:00Z" - }, { "id": 14, "title": "Is Prime", diff --git a/challenges/ownership/Cargo.toml b/challenges/ownership/Cargo.toml new file mode 100644 index 0000000..e884752 --- /dev/null +++ b/challenges/ownership/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/challenges/ownership/description.md b/challenges/ownership/description.md new file mode 100644 index 0000000..8a2fb81 --- /dev/null +++ b/challenges/ownership/description.md @@ -0,0 +1,62 @@ +Rust's ownership model is one of its most unique and powerful features, ensuring **memory safety** without needing a garbage collector. Ownership in Rust is governed by a set of rules that the compiler checks at compile time. Understanding these rules is crucial for writing efficient and safe Rust code. + +## Ownership Basics + +In Rust, each value has a variable that's called its **owner**. There can only be one owner at a time, and when the owner goes out of scope, the value is dropped. Here are the basic rules of ownership: + +1. Each value in Rust has a variable that's called its **owner**. +2. There can only be **one owner at a time**. +3. When the owner goes out of scope, the value will be **dropped** (no longer valid). + +### Example + +```rust +{ + let s = String::from("hello"); // s is the owner of the String +} // s goes out of scope and "hello" is dropped +``` + +## Borrowing + +Rust allows you to create **references** to a value, which lets you access it without taking ownership. This is called **borrowing**. Borrowing can be **immutable** or **mutable**. + +### Immutable References + +You can create multiple **immutable references** to a value, but you cannot have a mutable reference while immutable references exist. This allows you to read from the value without changing it. + +## Example + +```rust +fn main() { + let s1 = String::from("hello"); + + let len = calculate_length(&s1); // borrow s1 as immutable + println!("The length of '{}' is {}.", s1, len); +} + +fn calculate_length(s: &String) -> usize { // s is an immutable reference to a String + s.len() +} // s goes out of scope here, but since it does not have ownership of the String, nothing happens +``` + +## Challenge + +In this challenge, you will create a function `calculate_length` that takes an immutable reference to a `String`, calculates its length, and returns the length. + +The task is designed to help you understand the concepts of ownership and immutable borrowing in Rust. + +## Requirements + +- The `calculate_length` function should take **an immutable reference** to the input `String` and return its length. + +## Example + +```rust +let s1 = String::from("hello"); +let len = calculate_length(&s1); +assert_eq!(len, 5); +``` + +## Hints + +- The `String` type in Rust has a method `len()` which returns its length. diff --git a/challenges/ownership/src/lib.rs b/challenges/ownership/src/lib.rs new file mode 100644 index 0000000..1b2e105 --- /dev/null +++ b/challenges/ownership/src/lib.rs @@ -0,0 +1,3 @@ +pub fn calculate_length(s: &String) -> usize { + s.len() +} diff --git a/challenges/ownership/src/starter.rs b/challenges/ownership/src/starter.rs new file mode 100644 index 0000000..3a4fe66 --- /dev/null +++ b/challenges/ownership/src/starter.rs @@ -0,0 +1,3 @@ +pub fn calculate_length(s: &String) -> usize { + // Your code here... +} diff --git a/challenges/ownership/tests/tests.rs b/challenges/ownership/tests/tests.rs new file mode 100644 index 0000000..bb74d36 --- /dev/null +++ b/challenges/ownership/tests/tests.rs @@ -0,0 +1,37 @@ +#[cfg(test)] +mod tests { + use ownership::calculate_length; + + #[test] + fn test_calculate_length() { + assert_eq!( + calculate_length(&String::from("hello")), + 5, + "Expected the length of 'hello' to be 5" + ); + + assert_eq!( + calculate_length(&String::from("world")), + 5, + "Expected the length of 'world' to be 5" + ); + + assert_eq!( + calculate_length(&String::from("hello world")), + 11, + "Expected the length of 'hello world' to be 11" + ); + + assert_eq!( + calculate_length(&String::from("hello world!")), + 12, + "Expected the length of 'hello world!' to be 12" + ); + + assert_eq!( + calculate_length(&String::from("I'm a rust developer, and I love it!")), + 36, + "Expected the text to be 39 characters long" + ); + } +}