December Code

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.

New to this? How to prepare for placements — the method — and what the readiness test measures.

Rust topics, one page each

8 of the 8 Rust topics in the bank have a page so far; the diagnostic draws from all 8.

  1. Rust data types interview questions

    Rust's types as interviews test them: scalars and compounds, mutability, const and static, String versus &str, usize, inference and conversions.

  2. Rust ownership interview questions

    Rust ownership explained for interviews: the three rules, moves, Copy versus Clone, dropping early, memory without a GC and move closures.

  3. Rust borrowing and lifetimes interview questions

    Rust borrowing and lifetimes for interviews: references, the borrowing rules, dangling references, lifetime annotations, elision and slices.

  4. Rust structs, enums and pattern matching interview questions

    Rust structs, enums and matching for interviews: methods with impl, enums with data, Option, exhaustive match, if let, derive, destructuring.

  5. Rust error handling interview questions

    Rust error handling for interviews: Result and panic!, Option versus Result, the ? operator, custom error types, fallbacks and conversions.

  6. Rust traits and generics interview questions

    Rust traits and generics for interviews: defining traits, trait bounds, associated types, the orphan rule, generic structs, derive, From and Into.

  7. Rust iterators and collections interview questions

    Rust collections and iterators for interviews: Vec and HashMap, iter versus into_iter, lazy adapters, collect, sorting, closures and chains.

  8. Rust smart pointers interview questions

    Rust 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 — 30 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.

Ownership & Moves · mediumRUST-006

What does this Rust code print?

let a = 5;
let b = a;
let v = vec![1, 2];
let w = v;
println!("{} {}", a, b);
println!("{:?}", w);
  1. 15 5, then [1, 2]correct
  2. 2It does not compile: a was moved into b
  3. 3It does not compile: v was moved into w
  4. 45 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.

More on this topic: Rust ownership interview questions

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: 1 diagnostic a month and 15 problems a day. No card.

By Harshit · updated