File tree 5 files changed +79
-2
lines changed
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 @@ -164,6 +164,21 @@ They are not the same. There are two solutions:
164
164
1. Add a `return` ahead of `num * num;`
165
165
2. remove `;`, make it to be `num * num`"""
166
166
167
+ [[exercises ]]
168
+ name = " functions6"
169
+ path = " exercises/02_functions/functions6.rs"
170
+ mode = " compile"
171
+ hint = """
172
+ Hint FIX #1: Closures can capture variables defined in the outer context.
173
+
174
+ Hint FIX #2: Closures can infer both input and returned types, when they are not
175
+ specified in the signature. But the closure cannot be reused with different
176
+ input types.
177
+
178
+ Read more about closures in the rust book dedicated section:
179
+ https://doc.rust-lang.org/book/ch13-01-closures.html
180
+ """
181
+
167
182
# IF
168
183
169
184
[[exercises ]]
@@ -406,6 +421,20 @@ Can you figure out how?
406
421
407
422
Another hint: it has to do with the `&` character."""
408
423
424
+ [[exercises ]]
425
+ name = " move_semantics7"
426
+ path = " exercises/06_move_semantics/move_semantics7.rs"
427
+ mode = " compile"
428
+ hint = """
429
+ When a closure capture a variable to modify it, it borrows that variable as a
430
+ mutable reference. In this exercise, as the closure mutably borrows `counter`
431
+ and is called later, any attempt to reborrow `counter` in between will lead to
432
+ an error.
433
+
434
+ You cannot immutably borrow a variable if a mutable closure is
435
+ called later in the scope.
436
+ """
437
+
409
438
# STRUCTS
410
439
411
440
[[exercises ]]
You can’t perform that action at this time.
0 commit comments