Supported match expressions

According to Match expressions - The Rust Reference, a match expression can match against any kind of expression except a struct expression. But it seems like the value to the left of => in match arms must be a literal value like a bool, char, &str, integer, range, tuple, or array. But it doesn't seem that it can match against a floating point value (probably due to precision issues with comparing them), a variable, or a value constructed in the match arm (like a call to the vec! macro). Is that correct?

You're misreading or misunderstanding the reference. The expression where the "except struct expression" note is included is not what you match against but the value you match. In general you match a value against a pattern, which is the syntax that's allowed to the left of the =>. The expression-except-struct thing is placed where the "expr" stands in: match expr { pattern => ..., ...}.

The exception for struct expression is also only syntactical. The restriction is so that the { can terminate the expression. If you want to write a struct expression, you still can, as long as you put the whole thing in parentheses, e. g. match (Struct {field: value}) { _ => () }.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.