@aturon feedback on if

https://github.com/rust-lang/book/issues/9#issuecomment-167879371
This commit is contained in:
Steve Klabnik 2015-12-29 17:36:21 -05:00
parent db59879a46
commit 98bd2931dc
2 changed files with 3 additions and 13 deletions

View File

@ -13,7 +13,7 @@
- [Scalar Types](scalar-types.md) - [Scalar Types](scalar-types.md)
- [Compound Types](compound-types.md) - [Compound Types](compound-types.md)
- [Comments](comments.md) - [Comments](comments.md)
- [`if`](if.md) - [Control flow with `if`](if.md)
- [Loops](loops.md) - [Loops](loops.md)
- [Ownership & borrowing](ownership-and-borrowing.md) - [Ownership & borrowing](ownership-and-borrowing.md)

View File

@ -1,4 +1,4 @@
# if # Control flow with `if`
> Two roads diverged in a yellow wood, > Two roads diverged in a yellow wood,
> And sorry I could not travel both > And sorry I could not travel both
@ -8,13 +8,6 @@
> >
> - Robert Frost, “The Road Not Taken” > - Robert Frost, “The Road Not Taken”
One of the most primitive operations in computer programming is the ability to
branch between two different code paths. A program can look at a value, and
then decide: should I follow this path, or should I take the other? Theres
actually two different metaphors here: a tree, whose branches come from the
same, single trunk. And a road, which splits off into two, each going in a
different direction.
In Rust, there are a few ways to cause our code to branch. The most fundamental In Rust, there are a few ways to cause our code to branch. The most fundamental
way is by using `if`. An `if` expression gives us two paths forward, and asks way is by using `if`. An `if` expression gives us two paths forward, and asks
the question, “Which one should I take?” the question, “Which one should I take?”
@ -218,7 +211,4 @@ error: aborting due to previous error
Could not compile `branches`. Could not compile `branches`.
``` ```
`if` and `else` have incompatible types. This cant work. This also `if` and `else` have incompatible types. This cant work.
means that you almost certainly need an `else` when using `if` in
this way. If you dont, what would the value be if the condition was
false?