mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 23:10:30 +00:00
chore: Refactored to adhere to new rustlings version
This commit is contained in:
parent
1c27aeead9
commit
e59c65cf45
6 changed files with 40 additions and 19 deletions
|
@ -26,6 +26,8 @@ bin = [
|
||||||
{ name = "functions4_sol", path = "../solutions/02_functions/functions4.rs" },
|
{ name = "functions4_sol", path = "../solutions/02_functions/functions4.rs" },
|
||||||
{ name = "functions5", path = "../exercises/02_functions/functions5.rs" },
|
{ name = "functions5", path = "../exercises/02_functions/functions5.rs" },
|
||||||
{ name = "functions5_sol", path = "../solutions/02_functions/functions5.rs" },
|
{ name = "functions5_sol", path = "../solutions/02_functions/functions5.rs" },
|
||||||
|
{ name = "functions6", path = "../exercises/02_functions/functions6.rs" },
|
||||||
|
{ name = "functions6_sol", path = "../solutions/02_functions/functions6.rs" },
|
||||||
{ name = "if1", path = "../exercises/03_if/if1.rs" },
|
{ name = "if1", path = "../exercises/03_if/if1.rs" },
|
||||||
{ name = "if1_sol", path = "../solutions/03_if/if1.rs" },
|
{ name = "if1_sol", path = "../solutions/03_if/if1.rs" },
|
||||||
{ name = "if2", path = "../exercises/03_if/if2.rs" },
|
{ name = "if2", path = "../exercises/03_if/if2.rs" },
|
||||||
|
@ -60,6 +62,8 @@ bin = [
|
||||||
{ name = "move_semantics4_sol", path = "../solutions/06_move_semantics/move_semantics4.rs" },
|
{ name = "move_semantics4_sol", path = "../solutions/06_move_semantics/move_semantics4.rs" },
|
||||||
{ name = "move_semantics5", path = "../exercises/06_move_semantics/move_semantics5.rs" },
|
{ name = "move_semantics5", path = "../exercises/06_move_semantics/move_semantics5.rs" },
|
||||||
{ name = "move_semantics5_sol", path = "../solutions/06_move_semantics/move_semantics5.rs" },
|
{ name = "move_semantics5_sol", path = "../solutions/06_move_semantics/move_semantics5.rs" },
|
||||||
|
{ name = "move_semantics6", path = "../exercises/06_move_semantics/move_semantics6.rs" },
|
||||||
|
{ name = "move_semantics6_sol", path = "../solutions/06_move_semantics/move_semantics6.rs" },
|
||||||
{ name = "structs1", path = "../exercises/07_structs/structs1.rs" },
|
{ name = "structs1", path = "../exercises/07_structs/structs1.rs" },
|
||||||
{ name = "structs1_sol", path = "../solutions/07_structs/structs1.rs" },
|
{ name = "structs1_sol", path = "../solutions/07_structs/structs1.rs" },
|
||||||
{ name = "structs2", path = "../exercises/07_structs/structs2.rs" },
|
{ name = "structs2", path = "../exercises/07_structs/structs2.rs" },
|
||||||
|
|
|
@ -8,13 +8,12 @@
|
||||||
// Execute `rustlings hint functions6` or use the `hint` watch subcommand for
|
// Execute `rustlings hint functions6` or use the `hint` watch subcommand for
|
||||||
// some hints.
|
// some hints.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// TODO: ensure the definition of captured variable
|
||||||
let closure_1 = |input_var: u32| -> u32 {input_var + outer_var};
|
let closure_1 = |input_var: u32| -> u32 {input_var + outer_var};
|
||||||
println!("Closure#1 returns {}", closure_1(5));
|
println!("Closure#1 returns {}", closure_1(5));
|
||||||
|
|
||||||
let closure_2 = |input_var| println!("Closure#2 (input_var {})", input_var);
|
let closure_2 = |input_var| println!("Closure#2 (input_var {})", input_var);
|
||||||
closure_2(2);
|
closure_2(2);
|
||||||
closure_2("5");
|
closure_2("5"); // TODO: look at the captured variable type here
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,18 +9,16 @@
|
||||||
// Execute `rustlings hint move_semantics7` or use the `hint` watch subcommand
|
// Execute `rustlings hint move_semantics7` or use the `hint` watch subcommand
|
||||||
// for a hint.
|
// for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
|
|
||||||
let mut increment = || {
|
let mut increment = || {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
println!("counter: {}", counter);
|
println!("counter equals {}", counter);
|
||||||
};
|
};
|
||||||
|
|
||||||
increment();
|
increment();
|
||||||
let _reborrowed_counter = &counter;
|
let _reborrowed_counter = &counter; // TODO: figure out where to put this borrowing instruction
|
||||||
increment();
|
increment();
|
||||||
|
|
||||||
assert_eq!(counter, 2);
|
assert_eq!(counter, 2);
|
||||||
|
|
|
@ -189,8 +189,8 @@ There are two solutions:
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "functions6"
|
name = "functions6"
|
||||||
path = "exercises/02_functions/functions6.rs"
|
dir = "02_functions"
|
||||||
mode = "compile"
|
test = false
|
||||||
hint = """
|
hint = """
|
||||||
Hint FIX #1: Closures can capture variables defined in the outer context.
|
Hint FIX #1: Closures can capture variables defined in the outer context.
|
||||||
|
|
||||||
|
@ -199,8 +199,7 @@ specified in the signature. But the closure cannot be reused with different
|
||||||
input types.
|
input types.
|
||||||
|
|
||||||
Read more about closures in the rust book dedicated section:
|
Read more about closures in the rust book dedicated section:
|
||||||
https://doc.rust-lang.org/book/ch13-01-closures.html
|
https://doc.rust-lang.org/book/ch13-01-closures.html"""
|
||||||
"""
|
|
||||||
|
|
||||||
# IF
|
# IF
|
||||||
|
|
||||||
|
@ -408,17 +407,15 @@ to be adjusted."""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "move_semantics6"
|
name = "move_semantics6"
|
||||||
path = "exercises/06_move_semantics/move_semantics6.rs"
|
dir = "06_move_semantics"
|
||||||
mode = "compile"
|
test = false
|
||||||
hint = """
|
hint = """
|
||||||
When a closure capture a variable to modify it, it borrows that variable as a
|
When a closure captures a variable to modify it, it actually borrows that variable
|
||||||
mutable reference. In this exercise, as the closure mutably borrows `counter`
|
as a mutable reference. In this exercise, the closure mutably borrows the `counter`
|
||||||
and is called later, any attempt to reborrow `counter` in between will lead to
|
variable, thus, any attempt to borrow `counter` between closure calls leads to an error.
|
||||||
an error.
|
|
||||||
|
|
||||||
You cannot immutably borrow a variable if a mutable closure is
|
You cannot immutably borrow a variable if a mutable closure is
|
||||||
called later in the scope.
|
called later in the scope."""
|
||||||
"""
|
|
||||||
|
|
||||||
# STRUCTS
|
# STRUCTS
|
||||||
|
|
||||||
|
|
9
solutions/02_functions/functions6.rs
Normal file
9
solutions/02_functions/functions6.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
fn main() {
|
||||||
|
let outer_var = 1;
|
||||||
|
let closure_1 = |input_var: u32| -> u32 { input_var + outer_var };
|
||||||
|
println!("Closure#1 returns {}", closure_1(5));
|
||||||
|
|
||||||
|
let closure_2 = |input_var| println!("Closure#2 (input_var {})", input_var);
|
||||||
|
closure_2(2);
|
||||||
|
closure_2(5);
|
||||||
|
}
|
14
solutions/06_move_semantics/move_semantics6.rs
Normal file
14
solutions/06_move_semantics/move_semantics6.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
fn main() {
|
||||||
|
let mut counter = 0;
|
||||||
|
|
||||||
|
let mut increment = || {
|
||||||
|
counter += 1;
|
||||||
|
println!("counter equals {}", counter);
|
||||||
|
};
|
||||||
|
|
||||||
|
increment();
|
||||||
|
increment();
|
||||||
|
let _reborrowed_counter = &counter;
|
||||||
|
|
||||||
|
assert_eq!(counter, 2);
|
||||||
|
}
|
Loading…
Reference in a new issue