Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Tips and Traps

  1. match does a pattern matching which means each branch must be a pattern instead of an expression. Please refer to Patterns Are Not Expressions for differences between patterns and expressions.

  2. Avoid using the identifier pattern and _ in a pattern match especially when you work with a Enum. Keep in mind that explicit is better than implicit.

The following match statement won’t compile because 0 + 1 is NOT a pattern.

    0 + 1 => "how",
      ^ expected one of `...`, `..=`, `..`, `=>`, `if`, or `|`
expected one of `...`, `..=`, `..`, `=>`, `if`, or `|`, found `+`
unreachable expression
any code following this expression is unreachable
unreachable expression
1001

The following match statement is problematic as y is an identifier pattern which is irrefutable. In another words, y will match anything and the branch _ => "are" will never be reached.

"how"

Match Guards

You can use match guards to mimic an if/else or switch statement.

The following is a if/else like match statement even if it is not as concise.

"are"
"you"

Matching Literals

Matching Range

Destruct Struct and Matching

0
1
100
200