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 11# Functions
22
33Here, 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.
55
66## Further information
77
88- [ 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 33| Exercise | Book Chapter |
44| ---------------------- | ------------------- |
55| variables | §3.1 |
6- | functions | §3.3 |
6+ | functions | §3.3, §13.1 |
77| if | §3.5 |
88| primitive_types | §3.2, §4.3 |
99| vecs | §8.1 |
Original file line number Diff line number Diff line change @@ -187,6 +187,21 @@ There are two solutions:
1871871. Add the `return` keyword before `num * num;`
1881882. Remove the semicolon `;` after `num * num`"""
189189
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+
190205# IF
191206
192207[[exercises ]]
@@ -391,6 +406,20 @@ The first problem is that `get_char` is taking ownership of the string. So
391406Once you've fixed that, `string_uppercase`'s function signature will also need
392407to be adjusted."""
393408
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+
394423# STRUCTS
395424
396425[[exercises ]]
You can’t perform that action at this time.
0 commit comments