1. What is a struct?
IntermediateAnswer: Custom data type grouping related values
Struct groups related data: struct User { name: String, age: u32 }. Create: User { name: String::from("John"), age: 30 }. Three types: classic, tuple, unit.
24 questions that come up in Rust technical interviews, each with the answer and an explanation of why it is right.
Test yourself — 90 question bankAnswer: Custom data type grouping related values
Struct groups related data: struct User { name: String, age: u32 }. Create: User { name: String::from("John"), age: 30 }. Three types: classic, tuple, unit.
Answer: Systems programming language focused on safety and performance
Rust is a systems programming language focused on safety, speed, and concurrency. Prevents memory errors at compile time. No garbage collector. Created by Mozilla.
Answer: Ensure references valid as long as needed
Lifetimes ensure references remain valid. Prevent dangling references. Annotated: 'a. Compiler infers most. Explicit in function signatures, structs with references.
Answer: Memory safety, thread safety, no data races
Rust guarantees memory safety without garbage collection, prevents data races at compile time, and ensures thread safety through its ownership system. Zero-cost abstractions.
Answer: Classic (named fields), tuple, unit
Struct types: classic (struct Point { x: i32, y: i32 }), tuple (struct Color(i32, i32, i32)), unit (struct Marker;). Choose based on use case.
Answer: 'a syntax describing reference relationships
Lifetime annotation: fn function<'a>(x: &'a str, y: &'a str) -> &'a str. Doesn't change lifetimes, describes relationships. Compiler checks validity.
Answer: Lives for entire program duration
'static lives entire program. String literals have 'static. Use sparingly - often not needed. const and static items have 'static. Longest possible lifetime.
Answer: Type with multiple possible variants
Enum defines variants: enum Message { Quit, Move { x: i32, y: i32 }, Write(String) }. Each variant can hold different data. More powerful than C enums.
Answer: let x = 5;
Declare variables with let: let x = 5;. Variables are immutable by default. Use let mut x = 5; for mutable variables. Type inference or explicit: let x: i32 = 5;
Answer: Implements methods for types
impl implements methods: impl User { fn new() -> User { } }. Define methods, associated functions. Multiple impl blocks allowed. Use self for instance methods.
Answer: let is immutable, let mut is mutable
let creates immutable binding (default). let mut creates mutable binding. Immutability by default encourages safe, functional code. Explicit mut for changing values.
Answer: Compiler infers lifetimes following rules
Lifetime elision: compiler infers lifetimes in simple cases. Three rules for functions. Most cases covered. Explicit annotation when elision fails. Reduces boilerplate.
Answer: Structs holding references need lifetime parameters
Structs with references need lifetimes: struct Holder<'a> { reference: &'a str }. Ensures reference lives long enough. Multiple lifetimes possible. Part of type signature.
Answer: Each value has single owner, memory freed when owner goes out of scope
Ownership: each value has one owner. When owner goes out of scope, value is dropped (memory freed). Prevents memory leaks, double-frees. Core Rust concept.
Answer: self is instance, Self is type
self refers to instance (like this). Self refers to type. Method: fn method(&self), Associated function: fn new() -> Self. &self, &mut self, self for ownership.
Answer: Type safe to transfer between threads
Send indicates type safe to send between threads. Most types implement Send. Rc<T> does not (use Arc<T>). Auto trait. Ensures thread safety.
Answer: Defines shared behavior, like interfaces
Trait defines shared behavior: trait Speak { fn speak(&self); }. Implement: impl Speak for Dog. Like interfaces. Can have default implementations. Enable polymorphism.
Answer: Each value has one owner, only one owner at a time, value dropped when owner out of scope
Ownership rules: 1) Each value has one owner. 2) Only one owner at a time. 3) Value dropped when owner goes out of scope. Enforced at compile time.
Answer: Type safe to reference from multiple threads
Sync indicates &T safe to send between threads. Most types implement Sync. RefCell<T> does not. Auto trait. T: Sync means &T: Send. Ensures safe sharing.
Answer: impl TraitName for Type { }
Implement trait: impl Speak for Dog { fn speak(&self) { println!("Woof"); } }. Must implement all required methods. Can override defaults.
Answer: References allow using value without taking ownership
Borrowing creates references without taking ownership. Immutable: &x, mutable: &mut x. Multiple immutable or one mutable reference. Prevents data races at compile time.
Answer: &x is immutable reference, &mut x is mutable reference
&x creates immutable reference (read-only). &mut x creates mutable reference (can modify). Can have many &x or one &mut x, not both. Prevents data races.
Answer: Thread-safe reference-counted pointer
Arc<T> is thread-safe Rc<T>. Atomic reference counting. Use for shared ownership across threads. Immutable. Combine with Mutex for mutation. Essential concurrency primitive.
Answer: Automatically implements common traits
derive auto-implements traits: #[derive(Debug, Clone, PartialEq)]. Common traits: Debug, Clone, Copy, PartialEq, Eq. Saves boilerplate. Custom derive with proc macros.
The full Rust bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.
Take the Rust quiz