Rust

Error Handling

Result<T, E>, Option<T>, unwrap, expect, ?, custom errors, error propagation

18 면접 질문·
Junior
1

Which Rust type represents a value that may or may not be present?

답변

Option<T> is an enum that can be either Some(T) containing a value, or None indicating the absence of a value. This type replaces null values found in other languages and forces the developer to explicitly handle the case where the value is absent, thus avoiding null pointer errors at runtime.

2

Which Rust type represents an operation that can succeed or fail with an error?

답변

Result<T, E> is an enum with two variants: Ok(T) for success containing the return value, and Err(E) for failure containing the error. This type forces explicit error handling rather than using exceptions, making code more predictable and errors more visible.

3

What does the unwrap() method do on an Option<T>?

답변

unwrap() returns the value contained in Some(T) if it exists. If the Option is None, the program panics (panic!) and stops immediately. This method is convenient for prototyping but should be avoided in production as it does not gracefully handle the None case.

4

What is the difference between unwrap() and expect()?

5

What does the ? operator do in a function that returns Result<T, E>?

+15 면접 질문

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

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

무료로 시작하기