mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-09 20:29:18 +00:00
feat: Add move_semantics6.rs exercise
This commit is contained in:
parent
5002c54ffb
commit
b5205759f0
24
exercises/move_semantics/move_semantics6.rs
Normal file
24
exercises/move_semantics/move_semantics6.rs
Normal 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);
|
||||
}
|
||||
14
info.toml
14
info.toml
@ -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]]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user