Rust for placements: interview questions by topic
Rust is the language systems teams reach for when they need C-level speed without C's memory bugs, and it has moved from browsers and operating systems into databases, command-line tools, blockchains and backend services. Companies hiring for it know that few candidates arrive with years of Rust, so interviews concentrate on whether you understand the ideas that make it different, rather than on library trivia.
Those ideas are ownership, borrowing and lifetimes, which let the compiler prove memory safety without a garbage collector; enums and pattern matching, which replace null and many class hierarchies; Result and the ? operator, which make error handling explicit; and traits and generics, which replace inheritance. Being able to say why a piece of code does not compile, and how to fix it without reaching for clone everywhere, is the skill most Rust questions are really testing.
The pages below take one topic each, with answers whose code was compiled and run with rustc 1.98, and one question exactly as the diagnostic asks it. The diagnostic draws from every Rust topic in the bank and names the ones to read first.
On this page3 sections
Rust topics, one page each
- Rust data types interview questionsRust's types as interviews test them: scalars and compounds, mutability, const and static, String versus &str, usize, inference and conversions.
- Rust ownership interview questionsRust ownership explained for interviews: the three rules, moves, Copy versus Clone, dropping early, memory without a GC and move closures.
- Rust borrowing and lifetimes interview questionsRust borrowing and lifetimes for interviews: references, the borrowing rules, dangling references, lifetime annotations, elision and slices.
- Rust structs, enums and pattern matching interview questionsRust structs, enums and matching for interviews: methods with impl, enums with data, Option, exhaustive match, if let, derive, destructuring.
- Rust error handling interview questionsRust error handling for interviews: Result and panic!, Option versus Result, the ? operator, custom error types, fallbacks and conversions.
- Rust traits and generics interview questionsRust traits and generics for interviews: defining traits, trait bounds, associated types, the orphan rule, generic structs, derive, From and Into.
- Rust iterators and collections interview questionsRust collections and iterators for interviews: Vec and HashMap, iter versus into_iter, lazy adapters, collect, sorting, closures and chains.
- Rust smart pointers interview questionsRust smart pointers and threads for interviews: Box, Rc, RefCell and interior mutability, Arc across threads, Mutex guards and Weak references.
One question, exactly as the diagnostic asks it
From the Rust bank — 48 questions across 8 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.
What happens when this Rust code is compiled and run?
let a = 5;
let b = a;
let v = vec![1, 2];
let w = v;
println!("{} {}", a, b);
println!("{:?}", w);- 1correct
5 5, then [1, 2] - 2
It does not compile: a was moved into b
- 3
It does not compile: v was moved into w
- 4
5 0, then [1, 2]
Simple scalar types such as i32 implement the Copy trait, so let b = a copies the bits and both a and b are 5. A Vec owns a heap buffer and is not Copy, so let w = v moves it: v becomes unusable. But v is never used again, and a move is only an error when the old name is used after it, so the program compiles and prints 5 5, then [1, 2]. The option about a assumes every assignment moves. The option about v treats the move itself as the error. 5 0 imagines that a copied-from integer is reset, which never happens.
Measure it
Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.
10 Rust questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: a diagnostic in every subject, a retake a week and 15 problems a day. No card.