Rust Interview Questions

Rust interview topics — ownership, borrowing, Send/Sync, traits, and error types.

Core concepts

  1. Explain ownership in one minute — single owner, move vs borrow. — The Rust Book: Ownership
  2. &str vs String — borrowed view vs owned buffer. — The Rust Book: String slices
  3. Send vs Sync — thread-safe move vs shared reference across threads. — The Rust Book: Send and Sync
  4. Trait objects — dyn Trait dynamic dispatch vs monomorphized generics. — The Rust Book: Trait objects
  5. Panic vs Result — unrecoverable bugs vs expected failures. — The Rust Book: To panic or not

Concept videos

YouTube lessons for these topics. On a large screen they open in the reading panel next to the roadmap, same as the article links on each core concept.

Explanation

Rust interviews test mental models, not syntax trivia — can you explain why the borrow checker rejected code and how you would fix it?

Explain ownership in one minute

Tell the library book story: one owner, moves transfer responsibility, borrows are timed reading passes. Mention compile-time checks preventing use-after-free and data races — Rust’s headline value.

&str vs String

String owns heap UTF-8; &str borrows a slice. APIs taking &str are flexible; returning String allocates when building new text. Conversion between them (to_string, &s[..]) appears daily.

Send vs Sync

Send: safe to move to another thread. Sync: safe to share immutable references across threads. Async code requires futures to be Send when spawned on multithreaded runtimes — interviewers probe this with Rc vs Arc examples.

Trait objects vs generics

Generics monomorphize — static dispatch, fast, larger binaries. dyn Trait uses vtables — dynamic dispatch, smaller binary, runtime cost. Choose based on heterogeneity needs (plugin systems vs hot paths).

Panic vs Result

Panic for logic bugs and invariant violations — stop the world. Result for recoverable errors (I/O, parsing). Libraries prefer Result; binaries may panic on startup config failures with clear messages.

What Is It?

Interview focus: ownership moves, lifetimes intuition, trait objects vs generics, Result patterns.

Be ready to compare Rust safety model to GC languages.

Show you read compiler errors and adjust design.

Why Should I Care?

Infra and systems teams filter on borrow checker fluency.

Senior signals: choosing Arc/Mutex vs channels deliberately.

Real World Analogy

Rust interviews feel like explaining traffic laws after you have driven — the compiler already enforced them, now you teach why the rules exist.

Visual Mental Model

text
ownership | Send/Sync | traits | errors

Beginner Explanation

Practice explaining moves, borrowing, Send/Sync, and error types aloud with tiny snippets.

Smallest Possible Example

rust
let s1 = String::from("hi");
let s2 = s1;
// println!("{s1}"); // would not compile

Common Mistakes

❌ Memorizing answers without coding.

Live coding in Rust requires muscle memory for moves/borrows.

❌ Hand-waving Send/Sync as 'thread stuff'.

Explain cross-thread safety of types — common follow-up depth.

❌ Claiming Rust has no runtime costs.

Zero-cost abstractions still compile to real instructions — be precise.

Real Project Example

AWS Firecracker and Meta Diem-era teams interviewed on ownership heavily.

Startups building Rust APIs ask Axum + error handling design questions.

Build This Mini Project

Collect five snippets from this chapter, break each on purpose, and explain the compiler fix in writing.

Interview Question

What happens when you assign s1 to s2 for String?

Ownership moves — s1 is invalid afterward unless you clone. This prevents double-free at compile time.

Key Takeaways

  • Moves and borrows are top themes.
  • Send/Sync for async concurrency.
  • Result over panic in APIs.
  • Run snippets, do not recite.

Resources

The 60-second story

They ask you to explain Rust in the time it takes the coffee to cool. Start with the cash box, not with a feature list. There is one owner. Handing the box over is a move. Letting someone look is a borrow. Letting someone write means they hold the only pen. If you rush that and then talk about traits, the rest of the answer floats.

&str versus String is the follow-up they actually care about. String is the owned buffer, the box of bytes you are responsible for. &str is a window onto text, borrowed, usually UTF-8 already checked. You pass windows into functions so you do not clone the box for a print. You keep String when you need to store or change the text.

Send means “this value can move to another thread,” another truck. Sync means “a shared reference can be used from several trucks.” A type can be sendable and still not be safe to share. The interview wants to hear that those are two different permissions, like “you may take the box to the other shop” versus “several people may look at the policy on the wall.”

Trait objects, dyn Trait, are when you put different types behind one window and look up the job at runtime. Generics are when the compiler prints a specific copy of the function per type. Dynamic dispatch is flexible and a touch indirect. Monomorphization is bulky at compile time and direct at the call. You pick dyn when the set of types is open. You pick generics when you know the types and want the direct call.

Panic versus Result is the fire alarm versus the returned envelope. Panic is “this is a bug, stop.” Result is “this mailbox was empty, handle it.” If you unwrap every envelope, you turned the mail into fire alarms. That is a fine prototype and a bad library.

Map the cooldown.

  • Ownership in one minute = one box, moves and borrows.
  • String = the box. &str = the window.
  • Send = may move to another truck.
  • Sync = may be shared by reference across trucks.
  • dyn Trait = one window, lookup on the spot.
  • Generics = a printed form per type.
  • Panic = fire alarm. Result = an envelope you must open.
rust
fn echo(name: &str) -> String {
    format!("hi {name}")
}

The function borrows a window and returns a new box. That signature is an interview answer by itself.

These questions stick around because they are the design of the language, not trivia. If you can tell the cash-box story and then point at a function signature, you do not need to recite the book’s table of contents.

Mental model: a box, a window, a truck, an envelope. Practice that sequence until it fits in the time the coffee stays warm.

Threads in the same domain as this chapter — go deeper on iHateReading without leaving the roadmap.

Web scraping cover

Web scraping

Fetch and parse web data — the baseline skill behind browser-facing agents.

The whole fight for AI sandbox cover

The whole fight for AI sandbox

Why execution boundaries matter before you give agents shell or browser access.