
Collections
Vec<T>, String, HashMap, HashSet, iterators, collect, ownership with collections
1Which method allows adding an element to the end of a Vec<T>?
Which method allows adding an element to the end of a Vec<T>?
Risposta
The push() method adds an element to the end of the vector, increasing its size by 1. This operation has amortized O(1) complexity because the vector may need to reallocate memory if capacity is exceeded. To add multiple elements, extend() or append() can be used depending on the needs.
2What is the main difference between String and &str in Rust?
What is the main difference between String and &str in Rust?
Risposta
String is an owned type stored on the heap, mutable and resizable. &str is an immutable reference to a UTF-8 sequence, often called a 'string slice'. String owns its data and can be modified, while &str borrows existing data without owning it. This distinction is fundamental for memory management in Rust.
3How to create a new empty Vec<i32> in Rust?
How to create a new empty Vec<i32> in Rust?
Risposta
Vec::new() creates an empty vector without initial allocation. The vec![] macro can also create an empty vector, but Vec::new() is the standard explicit method. The generic type can be inferred or explicitly specified with turbofish syntax Vec::<i32>::new() or by type annotation.
What does the get() method return on a Vec<T>?
Which data structure to use for associating keys with values with O(1) average access?
+17 domande da colloquio
Altri argomenti di colloquio Rust
Rust Basics
Ownership & Borrowing
Structs & Enums
Error Handling
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
Padroneggia Rust per il tuo prossimo colloquio
Accedi a tutte le domande, flashcards, test tecnici, esercizi di code review e simulatori di colloquio.
Inizia gratis