Search⌘ K

Commas and Braces

Explore how to write match expressions in Rust by learning the flexible use of commas and braces in match arms. Understand when braces can be omitted and how optional commas simplify adding new match arms, enhancing code clarity in enums and pattern matching.

We'll cover the following...

There are some funny details about the arms of a match expression worth covering. We saw one way of writing them above. The expressions were blocks surrounded by their own expressions, and they had commas after each block. As a reminder:

Rust 1.40.0
match self.job {
Job::Teacher => {
format!("Hello, you're a teacher named {}", self.name)
},
Job::Scientist => {
format!("Hello, you're a scientist named {}", self.name)
},
}

There are two legal ways you can ...