File tree Expand file tree Collapse file tree 5 files changed +79
-2
lines changed Expand file tree Collapse file tree 5 files changed +79
-2
lines changed Original file line number Diff line number Diff line change 1
1
# Functions
2
2
3
3
Here, you'll learn how to write functions and how the Rust compiler can help you debug errors even
4
- in more complex code.
4
+ in more complex code. You will also learn what is the difference with closures.
5
5
6
6
## Further information
7
7
8
8
- [ How Functions Work] ( https://doc.rust-lang.org/book/ch03-03-how-functions-work.html )
9
+ - [ Closures] ( https://doc.rust-lang.org/book/ch13-01-closures.html )
Original file line number Diff line number Diff line change
1
+ // functions6.rs
2
+ //
3
+ // Here you can practice special functions called `closures`, that can capture
4
+ // variables of their parent context.
5
+ // Fix the code below to make it compile, without changing the two closure
6
+ // definitions.
7
+ //
8
+ // Execute `rustlings hint functions6` or use the `hint` watch subcommand for
9
+ // some hints.
10
+
11
+ // I AM NOT DONE
12
+
13
+ fn main ( ) {
14
+ let closure_1 = |input_var : u32 | -> u32 { input_var + outer_var} ;
15
+ println ! ( "Closure#1 returns {}" , closure_1( 5 ) ) ;
16
+
17
+ let closure_2 = |input_var| println ! ( "Closure#2 (input_var {})" , input_var) ;
18
+ closure_2 ( 2 ) ;
19
+ closure_2 ( "5" ) ;
20
+ }
Original file line number Diff line number Diff line change
1
+ // move_semantics6.rs
2
+ //
3
+ // Here you will practice how mutable/immutable borrowing works in the context
4
+ // of a closure.
5
+ //
6
+ // Try to fix this code to make it compile and not panic.
7
+ // You can't change anything except removing 1 line.
8
+ //
9
+ // Execute `rustlings hint move_semantics7` or use the `hint` watch subcommand
10
+ // for a hint.
11
+
12
+ // I AM NOT DONE
13
+
14
+ fn main ( ) {
15
+ let mut counter = 0 ;
16
+
17
+ let mut increment = || {
18
+ counter += 1 ;
19
+ println ! ( "counter: {}" , counter) ;
20
+ } ;
21
+
22
+ increment ( ) ;
23
+ let _reborrowed_counter = & counter;
24
+ increment ( ) ;
25
+
26
+ assert_eq ! ( counter, 2 ) ;
27
+ }
Original file line number Diff line number Diff line change 3
3
| Exercise | Book Chapter |
4
4
| ---------------------- | ------------------- |
5
5
| variables | §3.1 |
6
- | functions | §3.3 |
6
+ | functions | §3.3, §13.1 |
7
7
| if | §3.5 |
8
8
| primitive_types | §3.2, §4.3 |
9
9
| vecs | §8.1 |
Original file line number Diff line number Diff line change @@ -187,6 +187,21 @@ There are two solutions:
187
187
1. Add the `return` keyword before `num * num;`
188
188
2. Remove the semicolon `;` after `num * num`"""
189
189
190
+ [[exercises ]]
191
+ name = " functions6"
192
+ path = " exercises/02_functions/functions6.rs"
193
+ mode = " compile"
194
+ hint = """
195
+ Hint FIX #1: Closures can capture variables defined in the outer context.
196
+
197
+ Hint FIX #2: Closures can infer both input and returned types, when they are not
198
+ specified in the signature. But the closure cannot be reused with different
199
+ input types.
200
+
201
+ Read more about closures in the rust book dedicated section:
202
+ https://doc.rust-lang.org/book/ch13-01-closures.html
203
+ """
204
+
190
205
# IF
191
206
192
207
[[exercises ]]
@@ -391,6 +406,20 @@ The first problem is that `get_char` is taking ownership of the string. So
391
406
Once you've fixed that, `string_uppercase`'s function signature will also need
392
407
to be adjusted."""
393
408
409
+ [[exercises ]]
410
+ name = " move_semantics6"
411
+ path = " exercises/06_move_semantics/move_semantics6.rs"
412
+ mode = " compile"
413
+ hint = """
414
+ When a closure capture a variable to modify it, it borrows that variable as a
415
+ mutable reference. In this exercise, as the closure mutably borrows `counter`
416
+ and is called later, any attempt to reborrow `counter` in between will lead to
417
+ an error.
418
+
419
+ You cannot immutably borrow a variable if a mutable closure is
420
+ called later in the scope.
421
+ """
422
+
394
423
# STRUCTS
395
424
396
425
[[exercises ]]
You can’t perform that action at this time.
0 commit comments