1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-21 23:45:42 +02:00

Clarify and correct closure syntax slide (#2647)

Simplify the example, adding demonstration of return type annotation and
removing confusing "lambda" reference from speaker notes.
This commit is contained in:
Frances Wingerter 2025-02-20 21:50:38 +00:00 committed by GitHub
parent 4f8b09009a
commit e16dc70903
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,13 +8,13 @@ Closures are created with vertical bars: `|..| ..`.
```rust,editable ```rust,editable
fn main() { fn main() {
let value = Some(13); // Argument and return type can be inferred for lightweight syntax:
dbg!(value.map(|num| format!("{num}"))); let double_it = |n| n * 2;
dbg!(double_it(50));
let mut nums = vec![1, 10, 99, 24]; // Or we can specify types and bracket the body to be fully explicit:
// Sort even numbers first. let add_1f32 = |x: f32| -> f32 { x + 1.0 };
nums.sort_by_key(|v| if v % 2 == 0 { (0, *v) } else { (1, *v) }); dbg!(add_1f32(50.));
dbg!(nums);
} }
``` ```
@ -26,7 +26,8 @@ fn main() {
- Argument types are optional, and are inferred if not given. The return type is - Argument types are optional, and are inferred if not given. The return type is
also optional, but can only be written if using `{ .. }` around the body. also optional, but can only be written if using `{ .. }` around the body.
- The examples are both lambdas -- they do not capture anything from their - The examples can both be written as mere nested functions instead -- they do
environment. We will see captures next. not capture any variables from their lexical environment. We will see captures
next.
</details> </details>