Koskamo

Pattern matching with match

Document v1 · published by Koskamo editorial · updated 2026-10-04

Original source

The match construct compares a value against a series of patterns — literal values, variable names, wildcards, and more — and runs the code of the first arm that fits, like a coin-sorting machine; unlike if, the matched expression can be any type, not just a Boolean. Each arm's code is an expression whose value becomes the value of the whole match, and patterns can bind to the parts of the matched value, which is how you extract data out of enum variants such as Some(i) in an Option<T>. Matches are exhaustive: the compiler refuses to compile a match whose arms do not cover every possibility, so forgetting the None case of an Option<T> is a compile error rather than a runtime bug. For the remaining cases, end with a catch-all arm: a variable pattern that binds the value, or _ to match any value without binding it — which also avoids an unused-variable warning — and use the unit value () as the arm's code when nothing should happen. Scope: Rust. Reviewed October 4, 2026.

Source and provenance

Original source
doc.rust-lang.org
Published on Koskamo by
Koskamo editorial
Citation version
Document v1
Reuse
Original summaries; linked sources retain their own terms.

The original source supplies the underlying information. Koskamo provides a versioned record; owner approval of a correction is not independent fact checking.