
Smart Pointers
Box<T>, Rc<T>, Arc<T>, RefCell<T>, interior mutability, Deref, Drop traits
1What is the primary use case for Box<T> in Rust?
What is the primary use case for Box<T> in Rust?
Câu trả lời
Box<T> allocates data on the heap instead of the stack. This is useful for types with unknown size at compile time (recursive types), transferring ownership of large data without copying, or creating trait objects. The Box pointer itself has a fixed size on the stack.
2How to define a recursive type like a linked list in Rust?
How to define a recursive type like a linked list in Rust?
Câu trả lời
A direct recursive type like enum List { Cons(i32, List), Nil } does not compile because the compiler cannot determine the size. Using Box<List> creates an indirection with a fixed size (pointer), allowing the compiler to calculate the type's size.
3What is the main difference between Rc<T> and Arc<T>?
What is the main difference between Rc<T> and Arc<T>?
Câu trả lời
Rc (Reference Counted) uses a non-atomic reference counter, making it more performant but limited to a single thread. Arc (Atomically Reference Counted) uses atomic operations for the counter, enabling sharing across threads but with a slight performance overhead.
What does the Deref trait enable in Rust?
What is interior mutability in Rust?
+19 câu hỏi phỏng vấn
Các chủ đề phỏng vấn Rust khác
Rust Basics
Ownership & Borrowing
Structs & Enums
Error Handling
Collections
Modules & Packages
Traits
Generics
Lifetimes
Iterators & Closures
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í