All interview guides

Rust Interview Questions and Answers

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 bank

1. What is a struct?

Intermediate

Answer: 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.

2. What is Rust?

Beginner

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.

3. What are lifetimes?

Advanced

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.

4. What are Rust's main guarantees?

Beginner

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.

5. What are the three types of structs?

Intermediate

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.

6. What is lifetime annotation?

Advanced

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.

7. What is 'static lifetime?

Advanced

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.

8. What is an enum?

Intermediate

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.

9. How do you declare a variable?

Beginner

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;

10. What is impl?

Intermediate

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.

11. What is the difference between let and let mut?

Beginner

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.

12. What is lifetime elision?

Advanced

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.

13. What are generic lifetimes in structs?

Advanced

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.

14. What is ownership?

Beginner

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.

15. What is self vs Self?

Intermediate

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.

16. What is Send trait?

Advanced

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.

17. What is a trait?

Intermediate

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.

18. What are the ownership rules?

Beginner

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.

19. What is Sync trait?

Advanced

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.

20. How do you implement a trait?

Intermediate

Answer: impl TraitName for Type { }

Implement trait: impl Speak for Dog { fn speak(&self) { println!("Woof"); } }. Must implement all required methods. Can override defaults.

21. What is borrowing?

Beginner

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.

22. What is the difference between &x and &mut x?

Beginner

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.

23. What is Arc<T>?

Advanced

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.

24. What is derive?

Intermediate

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.

Ready to test yourself?

The full Rust bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the Rust quiz