Use match expression directly in guide.md
Use a match expression directly in the println statement, instead of creating a second variable.
This commit is contained in:
parent
93e589c872
commit
ce83a24b5f
1 changed files with 6 additions and 8 deletions
|
@ -1255,8 +1255,9 @@ version, if we had forgotten the `Greater` case, for example, our program would
|
||||||
have happily compiled. If we forget in the `match`, it will not. Rust helps us
|
have happily compiled. If we forget in the `match`, it will not. Rust helps us
|
||||||
make sure to cover all of our bases.
|
make sure to cover all of our bases.
|
||||||
|
|
||||||
`match` is also an expression, which means we can use it on the right hand side
|
`match` is also an expression, which means we can use it on the right
|
||||||
of a `let` binding. We could also implement the previous line like this:
|
hand side of a `let` binding or directly where an expression is
|
||||||
|
used. We could also implement the previous line like this:
|
||||||
|
|
||||||
```{rust}
|
```{rust}
|
||||||
fn cmp(a: int, b: int) -> Ordering {
|
fn cmp(a: int, b: int) -> Ordering {
|
||||||
|
@ -1269,18 +1270,15 @@ fn main() {
|
||||||
let x = 5i;
|
let x = 5i;
|
||||||
let y = 10i;
|
let y = 10i;
|
||||||
|
|
||||||
let result = match cmp(x, y) {
|
println!("{}", match cmp(x, y) {
|
||||||
Less => "less",
|
Less => "less",
|
||||||
Greater => "greater",
|
Greater => "greater",
|
||||||
Equal => "equal",
|
Equal => "equal",
|
||||||
};
|
});
|
||||||
|
|
||||||
println!("{}", result);
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
In this case, it doesn't make a lot of sense, as we are just making a temporary
|
Sometimes, it's a nice pattern.
|
||||||
string where we don't need to, but sometimes, it's a nice pattern.
|
|
||||||
|
|
||||||
# Looping
|
# Looping
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue