File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -237,6 +237,21 @@ in the book's section References and Borrowing':
237
237
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.
238
238
"""
239
239
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
+
240
255
# PRIMITIVE TYPES
241
256
242
257
[[exercises ]]
You can’t perform that action at this time.
0 commit comments