@@ -1,6 +1,6 @@
|
||||
# Control flow, part 1
|
||||
|
||||
All our programs so far have been pretty straightforward.
|
||||
All our programs so far have been pretty straightforward.\
|
||||
A sequence of instructions is executed from top to bottom, and that's it.
|
||||
|
||||
It's time to introduce some **branching**.
|
||||
@@ -23,7 +23,7 @@ This program will print `number is smaller than 5` because the condition `number
|
||||
### `else` clauses
|
||||
|
||||
Like most programming languages, Rust supports an optional `else` branch to execute a block of code when the condition in an
|
||||
`if` expression is false.
|
||||
`if` expression is false.\
|
||||
For example:
|
||||
|
||||
```rust
|
||||
@@ -38,7 +38,7 @@ if number < 5 {
|
||||
|
||||
## Booleans
|
||||
|
||||
The condition in an `if` expression must be of type `bool`, a **boolean**.
|
||||
The condition in an `if` expression must be of type `bool`, a **boolean**.\
|
||||
Booleans, just like integers, are a primitive type in Rust.
|
||||
|
||||
A boolean can have one of two values: `true` or `false`.
|
||||
@@ -67,12 +67,12 @@ error[E0308]: mismatched types
|
||||
```
|
||||
|
||||
This follows from Rust's philosophy around type coercion: there's no automatic conversion from non-boolean types to booleans.
|
||||
Rust doesn't have the concept of **truthy** or **falsy** values, like JavaScript or Python.
|
||||
Rust doesn't have the concept of **truthy** or **falsy** values, like JavaScript or Python.\
|
||||
You have to be explicit about the condition you want to check.
|
||||
|
||||
### Comparison operators
|
||||
|
||||
It's quite common to use comparison operators to build conditions for `if` expressions.
|
||||
It's quite common to use comparison operators to build conditions for `if` expressions.\
|
||||
Here are the comparison operators available in Rust when working with integers:
|
||||
|
||||
- `==`: equal to
|
||||
@@ -84,7 +84,7 @@ Here are the comparison operators available in Rust when working with integers:
|
||||
|
||||
## `if/else` is an expression
|
||||
|
||||
In Rust, `if` expressions are **expressions**, not statements: they return a value.
|
||||
In Rust, `if` expressions are **expressions**, not statements: they return a value.\
|
||||
That value can be assigned to a variable or used in other expressions. For example:
|
||||
|
||||
```rust
|
||||
@@ -96,11 +96,10 @@ let message = if number < 5 {
|
||||
};
|
||||
```
|
||||
|
||||
In the example above, each branch of the `if` evaluates to a string literal,
|
||||
which is then assigned to the `message` variable.
|
||||
In the example above, each branch of the `if` evaluates to a string literal,
|
||||
which is then assigned to the `message` variable.\
|
||||
The only requirement is that both `if` branches return the same type.
|
||||
|
||||
|
||||
## References
|
||||
|
||||
- The exercise for this section is located in `exercises/02_basic_calculator/03_if_else`
|
||||
- The exercise for this section is located in `exercises/02_basic_calculator/03_if_else`
|
||||
|
||||
Reference in New Issue
Block a user