-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add functions6.rs and move_semantics7.rs exercises about closures
- Loading branch information
Showing
5 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
# Functions | ||
|
||
Here, you'll learn how to write functions and how the Rust compiler can help you debug errors even | ||
in more complex code. | ||
in more complex code. You will also learn what is the difference with closures. | ||
|
||
## Further information | ||
|
||
- [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html) | ||
- [Closures](https://doc.rust-lang.org/book/ch13-01-closures.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// functions6.rs | ||
// | ||
// Here you can practice special functions called `closures`, that can capture | ||
// variables of their parent context. | ||
// Fix the code below to make it compile, without changing the two closure | ||
// definitions. | ||
// | ||
// Execute `rustlings hint functions6` or use the `hint` watch subcommand for | ||
// some hints. | ||
|
||
// I AM NOT DONE | ||
|
||
fn main() { | ||
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// move_semantics6.rs | ||
// | ||
// Here you will practice how mutable/immutable borrowing works in the context | ||
// of a closure. | ||
// | ||
// Try to fix this code to make it compile and not panic. | ||
// You can't change anything except removing 1 line. | ||
// | ||
// Execute `rustlings hint move_semantics7` or use the `hint` watch subcommand | ||
// for a hint. | ||
|
||
// I AM NOT DONE | ||
|
||
fn main() { | ||
let mut counter = 0; | ||
|
||
let mut increment = || { | ||
counter += 1; | ||
println!("counter: {}", counter); | ||
}; | ||
|
||
increment(); | ||
let _reborrowed_counter = &counter; | ||
increment(); | ||
|
||
assert_eq!(counter, 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters