Skip to content

Commit 5f32992

Browse files
committed
rollup merge of #21206: steveklabnik/expressions
Suggested here: http://stackoverflow.com/a/27962076/24817
2 parents 002d840 + fd603cd commit 5f32992

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/doc/trpl/functions.md

+18
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,23 @@ fn foo(x: i32) -> i32 {
142142
}
143143
```
144144

145+
The previous definition without `return` may look a bit strange if you haven't
146+
worked in an expression-based language before, but it becomes intutive over
147+
time. If this were production code, we wouldn't write it in that way anyway,
148+
we'd write this:
149+
150+
```rust
151+
fn foo(x: i32) -> i32 {
152+
if x < 5 {
153+
x
154+
} else {
155+
x + 1
156+
}
157+
}
158+
```
159+
160+
Because `if` is an expression, and it's the only expression in this function,
161+
the value will be the result of the `if`.
162+
145163
There are some additional ways to define functions, but they involve features
146164
that we haven't learned about yet, so let's just leave it at that for now.

0 commit comments

Comments
 (0)