mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-09 19:07:30 +02:00
28 lines
430 B
Markdown
28 lines
430 B
Markdown
|
# `if` expressions
|
||
|
|
||
|
You use `if` very similarly to how you would in other languages:
|
||
|
|
||
|
```rust,editable
|
||
|
fn main() {
|
||
|
let mut x = 10;
|
||
|
if x % 2 == 0 {
|
||
|
x = x / 2;
|
||
|
} else {
|
||
|
x = 3 * x + 1;
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
In addition, you can use it as an expression. This does the same as above:
|
||
|
|
||
|
```rust,editable
|
||
|
fn main() {
|
||
|
let mut x = 10;
|
||
|
x = if x % 2 == 0 {
|
||
|
x / 2
|
||
|
} else {
|
||
|
3 * x + 1
|
||
|
};
|
||
|
}
|
||
|
```
|