Skip to content

Commit d876ba2

Browse files
committed
Improve example for function parameters and body
The "desugaring" of patterns and return values is improved, featuring the full function now in the desugaring, not just its body, and using a less trivial body in the original function (including a let statement) in order to make the correlation of what part goes where more clear.
1 parent 8c77e8b commit d876ba2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/items/functions.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ Function parameters are irrefutable [patterns], so any pattern that is valid in
6363
an else-less `let` binding is also valid as a parameter:
6464

6565
```rust
66-
fn first((value, _): (i32, i32)) -> i32 { value }
66+
fn vector_len_squared((x, y): (i32, i32)) -> i32 {
67+
let sum = x + y;
68+
sum * sum
69+
}
6770
```
6871

6972
If the first parameter is a _SelfParam_, this indicates that the function is a
@@ -84,13 +87,14 @@ the body of the function will short-cut that implicit return, if reached.
8487

8588
For example, the function above behaves as if it was written as:
8689

87-
<!-- ignore: example expansion -->
88-
```rust,ignore
89-
// argument_0 is the actual first argument passed from the caller
90-
let (value, _) = argument_0;
91-
return {
92-
value
93-
};
90+
```rust
91+
fn vector_len_squared(argument: (i32, i32)) -> i32 {
92+
let (x, y) = argument;
93+
return {
94+
let sum = x + y;
95+
sum * sum
96+
};
97+
}
9498
```
9599

96100
Functions without a body block are terminated with a semicolon. This form

0 commit comments

Comments
 (0)