feat: Add move_semantics6.rs exercise

This commit is contained in:
Kallu-A 2022-01-08 01:28:00 +01:00
parent 5002c54ffb
commit b5205759f0
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,24 @@
// move_semantics6.rs
// Make me compile! `rustlings hint move_semantics6` for hints
// You can't change anything except adding or removing reference
// I AM NOT DONE
fn main() {
let data = "Rust is great!".to_string();
println!("{}", get_char(data));
string_uppercase(&data);
}
// Should not take ownership
fn get_char(data: String) -> char {
data.chars().last().unwrap()
}
// Should take ownership
fn string_uppercase(mut data: &String) {
data = &data.to_uppercase();
println!("{}", data);
}

View File

@ -221,6 +221,20 @@ in the book's section References and Borrowing':
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.
"""
[[exercises]]
name = "move_semantics6"
path = "exercises/move_semantics/move_semantics6.rs"
mode = "compile"
hint = """
Here if you want to find the answer by yourself, you can read in the book's section References and Borrowing:
https://doc.rust-lang.org/stable/book/ch04-02-references-and-borrowing.html
Else the first problem is that `get_char` is taking ownership of the string
So `data` is moved and can't be used for `string_uppercase`
- `get_char` should borrow data (use &)
One it's fix now is `string_uppercase` who's trying to change a borrowed value
so it's the opposite of `get_char` (remove &)
"""
# PRIMITIVE TYPES
[[exercises]]