Rust

Pattern Matching

Match expressions, if let, while let, destructuring, guards, @ bindings, exhaustiveness

18 gespreksvragen·
Mid-Level
1

What is the correct syntax for a basic match expression in Rust?

Antwoord

The match expression in Rust uses the match keyword followed by the value to analyze, then curly braces containing the different arms (patterns) separated by commas. Each arm uses the pattern followed by => and the code to execute. This syntax allows the compiler to verify that all possible cases are covered.

2

Which pattern should be used to capture all remaining values in a match?

Antwoord

The _ (underscore) pattern is the catch-all pattern in Rust. It matches any value without binding it to a variable. Alternatively, using a variable name (like other or n) captures the value. The _ is preferred when the value is not used as it clearly indicates the intent.

3

How does if let work compared to match?

Antwoord

if let is a shorthand syntax for a match that handles only one pattern and ignores the others. Instead of writing a full match with a _ catch-all arm, if let allows extracting a value concisely when only a specific case matters. It is particularly useful with Option and Result.

4

What is exhaustiveness in the context of Rust pattern matching?

5

How to destructure a tuple in a pattern match?

+15 gespreksvragen

Beheers Rust voor je volgende gesprek

Krijg toegang tot alle vragen, flashcards, technische tests, code review-oefeningen en gespreksimulatoren.

Begin gratis