Skip to content

Commit d83ee30

Browse files
committed
chore: Refactored to adhere to new rustlings version
1 parent 1c27aee commit d83ee30

File tree

6 files changed

+40
-19
lines changed

6 files changed

+40
-19
lines changed

dev/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ bin = [
2626
{ name = "functions4_sol", path = "../solutions/02_functions/functions4.rs" },
2727
{ name = "functions5", path = "../exercises/02_functions/functions5.rs" },
2828
{ name = "functions5_sol", path = "../solutions/02_functions/functions5.rs" },
29+
{ name = "functions6", path = "../exercises/02_functions/functions6.rs" },
30+
{ name = "functions6_sol", path = "../solutions/02_functions/functions6.rs" },
2931
{ name = "if1", path = "../exercises/03_if/if1.rs" },
3032
{ name = "if1_sol", path = "../solutions/03_if/if1.rs" },
3133
{ name = "if2", path = "../exercises/03_if/if2.rs" },
@@ -60,6 +62,8 @@ bin = [
6062
{ name = "move_semantics4_sol", path = "../solutions/06_move_semantics/move_semantics4.rs" },
6163
{ name = "move_semantics5", path = "../exercises/06_move_semantics/move_semantics5.rs" },
6264
{ name = "move_semantics5_sol", path = "../solutions/06_move_semantics/move_semantics5.rs" },
65+
{ name = "move_semantics6", path = "../exercises/06_move_semantics/move_semantics6.rs" },
66+
{ name = "move_semantics6_sol", path = "../solutions/06_move_semantics/move_semantics6.rs" },
6367
{ name = "structs1", path = "../exercises/07_structs/structs1.rs" },
6468
{ name = "structs1_sol", path = "../solutions/07_structs/structs1.rs" },
6569
{ name = "structs2", path = "../exercises/07_structs/structs2.rs" },

exercises/02_functions/functions6.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
// Execute `rustlings hint functions6` or use the `hint` watch subcommand for
99
// some hints.
1010

11-
// I AM NOT DONE
12-
1311
fn main() {
12+
// TODO: ensure the definition of captured variable
1413
let closure_1 = |input_var: u32| -> u32 {input_var + outer_var};
1514
println!("Closure#1 returns {}", closure_1(5));
1615

1716
let closure_2 = |input_var| println!("Closure#2 (input_var {})", input_var);
1817
closure_2(2);
19-
closure_2("5");
18+
closure_2("5"); // TODO: look at the captured variable type here
2019
}

exercises/06_move_semantics/move_semantics6.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99
// Execute `rustlings hint move_semantics7` or use the `hint` watch subcommand
1010
// for a hint.
1111

12-
// I AM NOT DONE
13-
1412
fn main() {
1513
let mut counter = 0;
1614

1715
let mut increment = || {
1816
counter += 1;
19-
println!("counter: {}", counter);
17+
println!("counter equals {}", counter);
2018
};
2119

2220
increment();
23-
let _reborrowed_counter = &counter;
21+
let _reborrowed_counter = &counter; // TODO: figure out where to put this borrowing instruction
2422
increment();
2523

2624
assert_eq!(counter, 2);

rustlings-macros/info.toml

+9-12
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ There are two solutions:
189189

190190
[[exercises]]
191191
name = "functions6"
192-
path = "exercises/02_functions/functions6.rs"
193-
mode = "compile"
192+
dir = "02_functions"
193+
test = false
194194
hint = """
195195
Hint FIX #1: Closures can capture variables defined in the outer context.
196196
@@ -199,8 +199,7 @@ specified in the signature. But the closure cannot be reused with different
199199
input types.
200200
201201
Read more about closures in the rust book dedicated section:
202-
https://doc.rust-lang.org/book/ch13-01-closures.html
203-
"""
202+
https://doc.rust-lang.org/book/ch13-01-closures.html"""
204203

205204
# IF
206205

@@ -408,17 +407,15 @@ to be adjusted."""
408407

409408
[[exercises]]
410409
name = "move_semantics6"
411-
path = "exercises/06_move_semantics/move_semantics6.rs"
412-
mode = "compile"
410+
dir = "06_move_semantics"
411+
test = false
413412
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.
413+
When a closure captures a variable to modify it, it actually borrows that variable
414+
as a mutable reference. In this exercise, the closure mutably borrows the `counter`
415+
variable, thus, any attempt to borrow `counter` between closure calls leads to an error.
418416
419417
You cannot immutably borrow a variable if a mutable closure is
420-
called later in the scope.
421-
"""
418+
called later in the scope."""
422419

423420
# STRUCTS
424421

solutions/02_functions/functions6.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
let outer_var = 1;
3+
let closure_1 = |input_var: u32| -> u32 {input_var + outer_var};
4+
println!("Closure#1 returns {}", closure_1(5));
5+
6+
let closure_2 = |input_var| println!("Closure#2 (input_var {})", input_var);
7+
closure_2(2);
8+
closure_2(5);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn main() {
2+
let mut counter = 0;
3+
4+
let mut increment = || {
5+
counter += 1;
6+
println!("counter equals {}", counter);
7+
};
8+
9+
increment();
10+
increment();
11+
let _reborrowed_counter = &counter;
12+
13+
assert_eq!(counter, 2);
14+
}

0 commit comments

Comments
 (0)