Skip to content

Commit

Permalink
mutable references
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 13, 2024
1 parent b4d2cc5 commit 569108f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions challenges/mutable-references/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "mutable-references"
version = "0.1.0"
edition = "2021"

[dependencies]
40 changes: 40 additions & 0 deletions challenges/mutable-references/description.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions challenges/mutable-references/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn append_suffix(s: &mut String, suffix: &str) {
s.push_str(suffix);
}
3 changes: 3 additions & 0 deletions challenges/mutable-references/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn append_suffix(s: &mut String, suffix: &str) {
// Your code here...
}
20 changes: 20 additions & 0 deletions challenges/mutable-references/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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!");
}
}

0 comments on commit 569108f

Please sign in to comment.