mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-10 12:49:18 +00:00
change from review
This commit is contained in:
parent
683cad89f3
commit
7030abe87d
@ -6,8 +6,8 @@
|
||||
|
||||
fn main() {
|
||||
let data = "Rust is great!".to_string();
|
||||
println!("{}", get_char(data));
|
||||
|
||||
get_char(data);
|
||||
|
||||
string_uppercase(&data);
|
||||
}
|
||||
@ -20,5 +20,6 @@ fn get_char(data: String) -> char {
|
||||
// Should take ownership
|
||||
fn string_uppercase(mut data: &String) {
|
||||
data = &data.to_uppercase();
|
||||
|
||||
println!("{}", data);
|
||||
}
|
||||
}
|
||||
|
||||
@ -230,10 +230,11 @@ To find the answer, you can consult the book section "References and Borrowing":
|
||||
https://doc.rust-lang.org/stable/book/ch04-02-references-and-borrowing.html
|
||||
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 &)
|
||||
"""
|
||||
`data` is moved to `get_char` first, meaning that `string_uppercase` cannot manipulate the data.
|
||||
Once you've fixed that, `string_uppercase`'s function signature will also need to be adjusted.
|
||||
Can you figure out how?
|
||||
|
||||
Another hint: it has to do with the `&` character."""
|
||||
|
||||
# PRIMITIVE TYPES
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user