diff --git a/Cargo.lock b/Cargo.lock index 26c2a1d..0efd066 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -762,6 +762,10 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "mutable-references" +version = "0.1.0" + [[package]] name = "mutable-variables" version = "0.1.0" diff --git a/challenges/challenges.json b/challenges/challenges.json index ce1e611..c6dc8d3 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -227,6 +227,18 @@ "created_at": "2024-06-13T00:00:00Z", "updated_at": "2024-06-13T00:00:00Z" }, + { + "id": 35, + "title": "Mutable References", + "slug": "mutable-references", + "short_description": "Implement Rust's ownership model with mutable references.", + "language": "RUST", + "difficulty": "BEGINNER", + "track": "RUST_BASICS", + "tags": ["ownership", "borrowing", "mutable references"], + "created_at": "2024-06-13T00:00:00Z", + "updated_at": "2024-06-13T00:00:00Z" + }, { "id": 2, "title": "Character counting string", diff --git a/challenges/mutable-references/Cargo.toml b/challenges/mutable-references/Cargo.toml new file mode 100644 index 0000000..54db61d --- /dev/null +++ b/challenges/mutable-references/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "mutable-references" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/challenges/mutable-references/description.md b/challenges/mutable-references/description.md new file mode 100644 index 0000000..4eab4c4 --- /dev/null +++ b/challenges/mutable-references/description.md @@ -0,0 +1,40 @@ +Rust's ownership model ensures memory safety without needing a garbage collector. In the previous challenge, you learned about **immutable references**. Now, let's dive into **mutable references**. + +## Mutable References + +**Mutable references** allow you to **modify** the value you are **borrowing**. **You can only have one mutable reference to a particular piece of data in a particular scope.** This prevents data races at compile time. + +### Example + +```rust +fn main() { + let mut s = String::from("hello"); + + change(&mut s); // borrow s as mutable + println!("{}", s); +} + +fn change(some_string: &mut String) { + some_string.push_str(", world"); +} +``` + +## Challenge + +Create a function `append_suffix` that takes a mutable reference to a `String` and appends a given suffix to it. + +## Requirements + +- The `append_suffix` function should take **a mutable reference** to the input `String` and append the given suffix to it. + +## Example + +```rust +let mut s2 = String::from("hello"); +append_suffix(&mut s2, " world"); +assert_eq!(s2, "hello world"); +``` + +## Hints + +- The `String` type in Rust has methods like `push_str` which can be useful for modifying strings. diff --git a/challenges/mutable-references/src/lib.rs b/challenges/mutable-references/src/lib.rs new file mode 100644 index 0000000..c9ac390 --- /dev/null +++ b/challenges/mutable-references/src/lib.rs @@ -0,0 +1,3 @@ +pub fn append_suffix(s: &mut String, suffix: &str) { + s.push_str(suffix); +} diff --git a/challenges/mutable-references/src/starter.rs b/challenges/mutable-references/src/starter.rs new file mode 100644 index 0000000..47a732d --- /dev/null +++ b/challenges/mutable-references/src/starter.rs @@ -0,0 +1,3 @@ +pub fn append_suffix(s: &mut String, suffix: &str) { + // Your code here... +} diff --git a/challenges/mutable-references/tests/tests.rs b/challenges/mutable-references/tests/tests.rs new file mode 100644 index 0000000..592c4be --- /dev/null +++ b/challenges/mutable-references/tests/tests.rs @@ -0,0 +1,20 @@ +#[cfg(test)] +mod tests { + use mutable_references::append_suffix; + + #[test] + fn test_append_suffix() { + let mut s = String::from("hello"); + append_suffix(&mut s, " world"); + assert_eq!(s, "hello world"); + + append_suffix(&mut s, "!"); + assert_eq!(s, "hello world!"); + + append_suffix(&mut s, "??"); + assert_eq!(s, "hello world!??"); + + append_suffix(&mut s, " I love Rust!"); + assert_eq!(s, "hello world!?? I love Rust!"); + } +}