Tech

Rust

Rust is a systems programming language focused on memory safety and performance without a garbage collector. Its ownership and borrowing system prevents data races and memory bugs at compile time, making it ideal for systems programming, WebAssembly, embedded devices, and performance-critical services. Interviews probe ownership rules, lifetimes, traits, enums with pattern matching, smart pointers, and the balance between safety and low-level control.

What you get

Questions

20

Difficulty

3 levels

Answer Formats

2

Use the toggle on each card to move between an interview-ready answer and a simpler explanation. Questions are sorted from beginner to advanced, and the keywords are highlighted. You can also blur the answers to practice recalling them from memory.

Questions

Practice the answers out loud.

All topics

Question 1

What is Rust and what are its key features?

Beginner

How to answer in an interview

Rust is a systems programming language focused on memory safety, concurrency, and performance. Its key features include an ownership system with borrowing rules that prevent data races and dangling pointers at compile time, zero-cost abstractions, pattern matching, traits for polymorphism, and no garbage collector. It's used for systems programming, WebAssembly, embedded systems, and performance-critical services.

Question 2

What is the difference between String and &str in Rust?

Beginner

How to answer in an interview

String is a growable, heap-allocated string type that owns its data and can be mutated. &str is an immutable string slice that is a reference to string data (either in a String, static memory, or elsewhere) and doesn't own the data. &str is preferred for function parameters when ownership isn't needed, and String for cases where ownership and mutation are required.

Question 3

What is the difference between Vec and array in Rust?

Beginner

How to answer in an interview

Arrays have a fixed size known at compile time and are stored on the stack. Vec (vector) is a growable, heap-allocated array that can change size at runtime. Slices (&[T]) are references to a contiguous sequence of elements, either from an array or Vec. Vec is the most commonly used collection for lists of variable length.

Question 4

What is the ? operator in Rust?

Beginner

How to answer in an interview

The ? operator unwraps a Result or Option, returning early with the error if one exists, or extracting the value otherwise. It dramatically reduces boilerplate compared to explicit match or if-let chains for error propagation.

Question 5

What is the Cargo build system?

Beginner

How to answer in an interview

Cargo is Rust's official build system and package manager. It handles compiling code, managing dependencies, running tests, benchmarking, and publishing crates (Rust packages) to crates.io. A Cargo.toml file defines project metadata, dependencies, and build configuration. Cargo simplifies the entire Rust development workflow from project creation to distribution.

Question 6

Explain Rust's ownership system.

Intermediate

How to answer in an interview

Rust's ownership system enforces three rules: each value has exactly one owner, ownership can be transferred (moved) to another variable, and the value is dropped when the owner goes out of scope. This eliminates the need for a garbage collector and prevents common memory bugs like use-after-free and double-free, all enforced at compile time.

Question 7

What are traits in Rust and how do they compare to interfaces?

Intermediate

How to answer in an interview

Traits define shared behavior that types can implement, similar to interfaces in other languages. They're used for polymorphism via trait bounds on generics and dynamic dispatch via trait objects. Rust traits can have default implementations, blanket implementations (applying a trait to all types meeting a condition), and associated types, offering more flexibility than traditional interfaces.

Question 8

What are enums and pattern matching in Rust?

Intermediate

How to answer in an interview

Enums in Rust are algebraic data types that can hold data in different variants, and pattern matching with the match expression lets you destructure and branch on enum variants exhaustively. The compiler ensures all variants are handled, preventing missed cases. Option and Result are two standard library enums used extensively for null safety and error handling.

Question 9

How does error handling work in Rust?

Intermediate

How to answer in an interview

Rust uses the Result<T, E> enum for recoverable errors and the panic! macro for unrecoverable ones. The ? operator propagates errors up the call stack concisely. Option<T> handles nullable values. Unlike exceptions, errors are explicit in function signatures, forcing callers to handle or propagate them. unwrap() and expect() are used in prototypes or when failure is truly unexpected.

Question 10

What are closures in Rust and how do they capture variables?

Intermediate

How to answer in an interview

Rust closures are anonymous functions that can capture variables from their environment. They come in three flavors: Fn borrows captured values immutably, FnMut borrows mutably, and FnOnce takes ownership of captured values (can only be called once). The compiler automatically chooses the most restrictive trait that works, and you can force capture behavior with move.

Question 11

How does Rust prevent data races?

Intermediate

How to answer in an interview

Rust prevents data races at compile time through its ownership and borrowing rules: at any given time you can have either one mutable reference or any number of immutable references to a piece of data, but not both. This makes it impossible to have two threads writing to the same data or one reading while another writes, eliminating data races without runtime checks.

Question 12

How does Rust handle concurrency compared to other languages?

Intermediate

How to answer in an interview

Rust offers 'Fearless Concurrency' by making data races compile-time errors through the ownership system. It supports both message passing via channels (mpsc) and shared state via Arc<Mutex<T>>. The Send and Sync traits automatically enforce thread safety. Unlike Go's goroutines, Rust uses OS threads by default, though async/await with runtimes like Tokio provides lightweight concurrency.

Question 13

What are macros in Rust?

Intermediate

How to answer in an interview

Rust has two main kinds of macros: declarative macros (macro_rules!) that pattern-match and expand code, and procedural macros that manipulate token streams. Derive macros automatically generate trait implementations (like #[derive(Debug)]). Macros are expanded at compile time, enabling code generation and DSLs without runtime overhead.

Question 14

What is the difference between Box, Rc, and Arc?

Intermediate

How to answer in an interview

Box<T> gives single ownership of a heap-allocated value, dropped when the Box goes out of scope. Rc<T> enables shared ownership via reference counting but is single-threaded. Arc<T> (Atomic Reference Counted) is the thread-safe version of Rc, using atomic operations for the reference count. Use Box for unique ownership, Rc for shared single-thread data, and Arc for shared multi-thread data.

Question 15

How does async/await work in Rust?

Intermediate

How to answer in an interview

Rust's async/await is syntactic sugar for writing functions that return a Future, which is a state machine that polls to completion. Unlike Go or JavaScript, Rust doesn't include an async runtime — you must choose one (like Tokio or async-std) to execute futures. This keeps the standard library lean while giving flexibility in how concurrency is managed.

Question 16

What are borrowing and lifetimes in Rust?

Advanced

How to answer in an interview

Borrowing lets you reference a value without taking ownership, either immutably (multiple readers allowed) or mutably (one writer, no readers). Lifetimes are compile-time annotations that describe how long references are valid, ensuring no reference outlives the data it points to. The compiler uses lifetime elision rules to reduce boilerplate in common cases.

Question 17

What are smart pointers in Rust?

Advanced

How to answer in an interview

Smart pointers in Rust manage heap-allocated data with additional behavior beyond raw references. Box<T> provides single ownership on the heap. Rc<T> and Arc<T> enable shared ownership (reference counted, with Arc being thread-safe). RefCell<T> provides runtime borrow checking. They're used when the ownership and borrowing rules can't be satisfied by the compile-time system alone.

Question 18

What are the Send and Sync traits in Rust?

Advanced

How to answer in an interview

Send indicates a type can be transferred to another thread, and Sync indicates a type can be safely shared between threads via references. They're marker traits with no methods — the compiler automatically implements them when safe. This ensures thread safety is enforced at compile time without runtime overhead, making data races impossible by construction.

Question 19

What are lifetimes in more detail and when do you need to annotate them?

Advanced

How to answer in an interview

Lifetime annotations (like 'a) describe the scope for which a reference is valid. The compiler can often infer lifetimes automatically (lifetime elision), but you need explicit annotations when a function returns a reference, when structs hold references, or when multiple references interact in ways the compiler can't deduce. They ensure references never outlive the data they point to.

Question 20

What is unsafe Rust and when is it used?

Advanced

How to answer in an interview

Unsafe Rust allows operations that the compiler can't verify as safe, including dereferencing raw pointers, calling foreign functions (FFI), accessing mutable statics, and implementing unsafe traits. It's used when you need low-level control that the safety system can't express, such as interfacing with C libraries or implementing performance-critical data structures. All unsafe code should be carefully audited and encapsulated.

More in Tech

Keep browsing related topics.

Browse all