Skip to content

Commit 3f0e130

Browse files
authored
feat: Add move_semantics6.rs exercise (#908)
1 parent c80ad08 commit 3f0e130

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// move_semantics6.rs
2+
// Make me compile! `rustlings hint move_semantics6` for hints
3+
// You can't change anything except adding or removing references
4+
5+
// I AM NOT DONE
6+
7+
fn main() {
8+
let data = "Rust is great!".to_string();
9+
10+
get_char(data);
11+
12+
string_uppercase(&data);
13+
}
14+
15+
// Should not take ownership
16+
fn get_char(data: String) -> char {
17+
data.chars().last().unwrap()
18+
}
19+
20+
// Should take ownership
21+
fn string_uppercase(mut data: &String) {
22+
data = &data.to_uppercase();
23+
24+
println!("{}", data);
25+
}

info.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,21 @@ in the book's section References and Borrowing':
237237
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.
238238
"""
239239

240+
[[exercises]]
241+
name = "move_semantics6"
242+
path = "exercises/move_semantics/move_semantics6.rs"
243+
mode = "compile"
244+
hint = """
245+
To find the answer, you can consult the book section "References and Borrowing":
246+
https://doc.rust-lang.org/stable/book/ch04-02-references-and-borrowing.html
247+
The first problem is that `get_char` is taking ownership of the string.
248+
So `data` is moved and can't be used for `string_uppercase`
249+
`data` is moved to `get_char` first, meaning that `string_uppercase` cannot manipulate the data.
250+
Once you've fixed that, `string_uppercase`'s function signature will also need to be adjusted.
251+
Can you figure out how?
252+
253+
Another hint: it has to do with the `&` character."""
254+
240255
# PRIMITIVE TYPES
241256

242257
[[exercises]]

0 commit comments

Comments
 (0)