
Generics
Generic functions, structs, enums, trait bounds, where clause, monomorphization
1What is the correct syntax for declaring a generic function in Rust?
What is the correct syntax for declaring a generic function in Rust?
Câu trả lời
In Rust, generic type parameters are declared using angle brackets after the function name. The syntax fn name<T>(param: T) indicates that T is a type parameter that will be determined at usage. The compiler then generates specialized code for each concrete type used when calling the function.
2How to declare a generic struct with a field of type T?
How to declare a generic struct with a field of type T?
Câu trả lời
A generic struct is declared with the type parameter in angle brackets after the struct name. The syntax struct Name<T> { field: T } allows creating a reusable structure with different types. The type T can then be used in fields and methods associated with this struct.
3What is monomorphization in Rust?
What is monomorphization in Rust?
Câu trả lời
Monomorphization is the process by which the Rust compiler generates specialized machine code for each concrete type used with a generic. For example, if a generic function is called with i32 and String, the compiler creates two distinct versions of that function. This ensures optimal performance without runtime overhead.
How to add a trait bound on a generic parameter T?
What is the difference between impl<T> Struct<T> and impl Struct<i32> syntax?
+17 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
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í