What you have written is not only different from what was given in the example, it is also completely useless.
match
can either be used as an expression, or as a statement right away. In the first syntax, you are using match
as an expression. Is it odd? Not if you are used to functional programming (for instance, you would have the same pattern in OCaml), but if you come from imperative language it can be a bit puzzling.
In the second case, this does not even mean anything because match arms should be expressions, not statements. So, to make it work, you should at least do something like
match guess.trim().parse() {
Ok(num) => {
let a = num;
}
Err(_) => continue,
}
But then, it's easy to understand what's wrong: the variable a
has a lifetime that is less that the match
scope, ie. the match
scope outlives a
, meaning that after the match
statement, a
is undefined.
See this playground example, where it just does what you expect, as opposed to this playground example which does not even compile. If you look, besides the "this does not make sense" error, there is a "this variable in undefined" error.
Besides, the idea of this pattern is that if you read the code quickly, you start by seeing a let
pattern (which is a bit cumbersome), because that is the main point of that statement, and not a conditional flow instruction, because that is not the important part of that statement.