
Error Handling
Result<T, E>, Option<T>, unwrap, expect, ?, custom errors, error propagation
1Which Rust type represents a value that may or may not be present?
Which Rust type represents a value that may or may not be present?
Câu trả lời
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.
2Which Rust type represents an operation that can succeed or fail with an error?
Which Rust type represents an operation that can succeed or fail with an error?
Câu trả lời
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.
3What does the unwrap() method do on an Option<T>?
What does the unwrap() method do on an Option<T>?
Câu trả lời
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.
What is the difference between unwrap() and expect()?
What does the ? operator do in a function that returns Result<T, E>?
+15 câu hỏi phỏng vấn
Các chủ đề phỏng vấn Rust khác
Rust Basics
Ownership & Borrowing
Structs & Enums
Collections
Modules & Packages
Traits
Generics
Lifetimes
Iterators & Closures
Smart Pointers
Concurrency Basics
async/await
Testing
Cargo & Ecosystem
Pattern Matching
Macros
Serde & Serialization
Unsafe Rust
Advanced Traits
Advanced Lifetimes
Type System
Tokio & Async I/O
Performance Optimization
Memory Management
Web Frameworks
Database Integration
Rust Design Patterns
Nắm vững Rust cho lần phỏng vấn tiếp theo
Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.
Bắt đầu miễn phí