Skip to content

Commit 5f2aff6

Browse files
committed
feat: add functions6.rs and move_semantics7.rs exercises about closures
1 parent c306792 commit 5f2aff6

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

exercises/02_functions/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Functions
22

33
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.
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)

exercises/02_functions/functions6.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

exercises/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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 |

info.toml

+29
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,21 @@ They are not the same. There are two solutions:
164164
1. Add a `return` ahead of `num * num;`
165165
2. remove `;`, make it to be `num * num`"""
166166

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+
167182
# IF
168183

169184
[[exercises]]
@@ -406,6 +421,20 @@ Can you figure out how?
406421
407422
Another hint: it has to do with the `&` character."""
408423

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+
409438
# STRUCTS
410439

411440
[[exercises]]

0 commit comments

Comments
 (0)