Rust

Pattern Matching

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

18 면접 질문·
Mid-Level
1

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

답변

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?

답변

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?

답변

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 면접 질문

다음 면접을 위해 Rust을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기