mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Compare commits
7 commits
e7cf38b4b0
...
2ce13b1d2e
Author | SHA1 | Date | |
---|---|---|---|
2ce13b1d2e | |||
8d0aa11a35 | |||
e2674498c6 | |||
3200581d4d | |||
6afc4840b4 | |||
93aef73eb5 | |||
5f2aff6783 |
|
@ -2550,6 +2550,15 @@
|
|||
"contributions": [
|
||||
"content"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "gerases",
|
||||
"name": "gerases",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/8953623?v=4",
|
||||
"profile": "https://github.com/gerases",
|
||||
"contributions": [
|
||||
"content"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 8,
|
||||
|
|
|
@ -360,6 +360,9 @@ authors.
|
|||
<td align="center" valign="top" width="12.5%"><a href="https://github.com/neuschaefer"><img src="https://avatars.githubusercontent.com/u/1021512?v=4?s=100" width="100px;" alt="J. Neuschäfer"/><br /><sub><b>J. Neuschäfer</b></sub></a><br /><a href="https://github.com/rust-lang/rustlings/commits?author=neuschaefer" title="Code">💻</a></td>
|
||||
<td align="center" valign="top" width="12.5%"><a href="https://scooterhacking.org"><img src="https://avatars.githubusercontent.com/u/58905488?v=4?s=100" width="100px;" alt="Bastian Pedersen"/><br /><sub><b>Bastian Pedersen</b></sub></a><br /><a href="#content-bastianpedersen" title="Content">🖋</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" valign="top" width="12.5%"><a href="https://github.com/gerases"><img src="https://avatars.githubusercontent.com/u/8953623?v=4?s=100" width="100px;" alt="gerases"/><br /><sub><b>gerases</b></sub></a><br /><a href="#content-gerases" title="Content">🖋</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
20
exercises/02_functions/functions6.rs
Normal file
20
exercises/02_functions/functions6.rs
Normal file
|
@ -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");
|
||||
}
|
27
exercises/06_move_semantics/move_semantics7.rs
Normal file
27
exercises/06_move_semantics/move_semantics7.rs
Normal file
|
@ -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);
|
||||
}
|
|
@ -36,7 +36,7 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
|||
let team_2_score: u8 = v[3].parse().unwrap();
|
||||
// TODO: Populate the scores table with details extracted from the
|
||||
// current line. Keep in mind that goals scored by team_1
|
||||
// will be the number of goals conceded from team_2, and similarly
|
||||
// will be the number of goals conceded by team_2, and similarly
|
||||
// goals scored by team_2 will be the number of goals conceded by
|
||||
// team_1.
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
| Exercise | Book Chapter |
|
||||
| ---------------------- | ------------------- |
|
||||
| variables | §3.1 |
|
||||
| functions | §3.3 |
|
||||
| functions | §3.3, §13.1 |
|
||||
| if | §3.5 |
|
||||
| primitive_types | §3.2, §4.3 |
|
||||
| vecs | §8.1 |
|
||||
|
|
29
info.toml
29
info.toml
|
@ -164,6 +164,21 @@ They are not the same. There are two solutions:
|
|||
1. Add a `return` ahead of `num * num;`
|
||||
2. remove `;`, make it to be `num * num`"""
|
||||
|
||||
[[exercises]]
|
||||
name = "functions6"
|
||||
path = "exercises/02_functions/functions6.rs"
|
||||
mode = "compile"
|
||||
hint = """
|
||||
Hint FIX #1: Closures can capture variables defined in the outer context.
|
||||
|
||||
Hint FIX #2: Closures can infer both input and returned types, when they are not
|
||||
specified in the signature. But the closure cannot be reused with different
|
||||
input types.
|
||||
|
||||
Read more about closures in the rust book dedicated section:
|
||||
https://doc.rust-lang.org/book/ch13-01-closures.html
|
||||
"""
|
||||
|
||||
# IF
|
||||
|
||||
[[exercises]]
|
||||
|
@ -406,6 +421,20 @@ Can you figure out how?
|
|||
|
||||
Another hint: it has to do with the `&` character."""
|
||||
|
||||
[[exercises]]
|
||||
name = "move_semantics7"
|
||||
path = "exercises/06_move_semantics/move_semantics7.rs"
|
||||
mode = "compile"
|
||||
hint = """
|
||||
When a closure capture a variable to modify it, it borrows that variable as a
|
||||
mutable reference. In this exercise, as the closure mutably borrows `counter`
|
||||
and is called later, any attempt to reborrow `counter` in between will lead to
|
||||
an error.
|
||||
|
||||
You cannot immutably borrow a variable if a mutable closure is
|
||||
called later in the scope.
|
||||
"""
|
||||
|
||||
# STRUCTS
|
||||
|
||||
[[exercises]]
|
||||
|
|
Loading…
Reference in a new issue