-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} |