match
expressions
The match
keyword
is used to match a value against one or more patterns. In that sense, it works
like a series of if let
expressions:
fn main() { match std::env::args().next().as_deref() { Some("cat") => println!("Will do cat things"), Some("ls") => println!("Will ls some files"), Some("mv") => println!("Let's move some files"), Some("rm") => println!("Uh, dangerous!"), None => println!("Hmm, no program name?"), _ => println!("Unknown program name!"), } }
Like if let
, each match arm must have the same type. The type is the last
expression of the block, if any. In the example above, the type is ()
.
See pattern matching for more details on patterns in Rust.
- Save the match expression to a variable and print it out.
- Remove
.as_deref()
and explain the error.std::env::args().next()
returns anOption<String>
, but we cannot match againstString
.as_deref()
transforms anOption<T>
toOption<&T::Target>
. In our case, this turnsOption<String>
intoOption<&str>
.- We can now use pattern matching to match against the
&str
insideOption
.