Skip to content

Commit c46812f

Browse files
committed
auto merge of #18120 : jrincayc/rust/match_exp, r=thestinger
Use a match expression directly in the println statement, instead of creating a second variable. It seems weird that the current guide.md complains about creating an extra variable, when the same feature could be demonstrated without creating the extra variable.
2 parents d8cf023 + ce83a24 commit c46812f

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/doc/guide.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,8 +1255,9 @@ version, if we had forgotten the `Greater` case, for example, our program would
12551255
have happily compiled. If we forget in the `match`, it will not. Rust helps us
12561256
make sure to cover all of our bases.
12571257

1258-
`match` is also an expression, which means we can use it on the right hand side
1259-
of a `let` binding. We could also implement the previous line like this:
1258+
`match` is also an expression, which means we can use it on the right
1259+
hand side of a `let` binding or directly where an expression is
1260+
used. We could also implement the previous line like this:
12601261

12611262
```{rust}
12621263
fn cmp(a: int, b: int) -> Ordering {
@@ -1269,18 +1270,15 @@ fn main() {
12691270
let x = 5i;
12701271
let y = 10i;
12711272
1272-
let result = match cmp(x, y) {
1273+
println!("{}", match cmp(x, y) {
12731274
Less => "less",
12741275
Greater => "greater",
12751276
Equal => "equal",
1276-
};
1277-
1278-
println!("{}", result);
1277+
});
12791278
}
12801279
```
12811280

1282-
In this case, it doesn't make a lot of sense, as we are just making a temporary
1283-
string where we don't need to, but sometimes, it's a nice pattern.
1281+
Sometimes, it's a nice pattern.
12841282

12851283
# Looping
12861284

0 commit comments

Comments
 (0)