mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29:18 +00:00
change from review
This commit is contained in:
parent
683cad89f3
commit
7030abe87d
@ -6,8 +6,8 @@
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let data = "Rust is great!".to_string();
|
let data = "Rust is great!".to_string();
|
||||||
println!("{}", get_char(data));
|
|
||||||
|
|
||||||
|
get_char(data);
|
||||||
|
|
||||||
string_uppercase(&data);
|
string_uppercase(&data);
|
||||||
}
|
}
|
||||||
@ -20,5 +20,6 @@ fn get_char(data: String) -> char {
|
|||||||
// Should take ownership
|
// Should take ownership
|
||||||
fn string_uppercase(mut data: &String) {
|
fn string_uppercase(mut data: &String) {
|
||||||
data = &data.to_uppercase();
|
data = &data.to_uppercase();
|
||||||
|
|
||||||
println!("{}", data);
|
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
|
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.
|
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`
|
So `data` is moved and can't be used for `string_uppercase`
|
||||||
- `get_char` should borrow data (use &)
|
`data` is moved to `get_char` first, meaning that `string_uppercase` cannot manipulate the data.
|
||||||
One it's fix now is `string_uppercase` who's trying to change a borrowed value
|
Once you've fixed that, `string_uppercase`'s function signature will also need to be adjusted.
|
||||||
so it's the opposite of `get_char` (remove &)
|
Can you figure out how?
|
||||||
"""
|
|
||||||
|
Another hint: it has to do with the `&` character."""
|
||||||
|
|
||||||
# PRIMITIVE TYPES
|
# PRIMITIVE TYPES
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user