Skip to content

Clarify function return style. #21206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/doc/trpl/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,23 @@ fn foo(x: i32) -> i32 {
}
```

The previous definition without `return` may look a bit strange if you haven't
worked in an expression-based language before, but it becomes intutive over
time. If this were production code, we wouldn't write it in that way anyway,
we'd write this:

```rust
fn foo(x: i32) -> i32 {
if x < 5 {
x
} else {
x + 1
}
}
```

Because `if` is an expression, and it's the only expression in this function,
the value will be the result of the `if`.

There are some additional ways to define functions, but they involve features
that we haven't learned about yet, so let's just leave it at that for now.