Rust conditionals

From wikinotes
Revision as of 23:27, 6 February 2023 by Will (talk | contribs) (Created page with " = Pattern Matching = <blockquote> Like a switch statement,<br> but the compiler ensures the entire valid range of items is checked for.<br> especially useful for enums. <syntaxhighlight lang="rust"> // if num is '1', returns 'a' // if num is >2, returns 'c' let result = match num { 1 => "a", 2 => "b", _ => "c", } </syntaxhighlight> </blockquote><!-- Pattern Matching -->")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Pattern Matching

Like a switch statement,
but the compiler ensures the entire valid range of items is checked for.
especially useful for enums.

// if num is '1', returns 'a'
// if num is >2, returns 'c'
let result = match num {
    1 => "a",
    2 => "b",
    _ => "c",
}