Rust Interview Questions: Traits
Reviewed by Mark Dickie · Last updated
4 questions on this page you can answer and see marked on the spot. Go to the first one
Traits in Rust are interfaces that define shared behavior a type can implement. For a traits-focused interview, you need to know how to define a trait with associated functions and default method bodies, implement it for concrete types, and apply trait bounds to generics so the compiler can prove the required methods exist. Candidates are commonly tested on the difference between static dispatch via trait bounds (fn foo<T: Trait>(x: T)) and dynamic dispatch via trait objects (&dyn Trait), including the cost and constraints of each.
| Concept | Key Point to Recall |
|---|---|
| Default methods | A trait can provide a body; impls inherit it unless overridden. |
| Associated types | Declared in the trait; each impl fixes the concrete type for each one (type Item = T;). |
| Trait bounds | T: Trait constrains generics; compound bounds use + syntax. |
dyn Trait vs impl Trait | dyn is runtime dispatch behind a fat pointer; impl is compile-time monomorphization. |
| Object safety | Methods must not return Self, take self by value, or have generic type parameters, and the trait must not require Self: Sized, for dyn Trait to work. |
What does a Rust traits interview usually test?
- Defining a trait with required and default methods, then implementing it correctly for a struct.
- Writing generic functions with trait bounds, including
whereclauses for readability. - Explaining when trait objects (
dyn Trait) are object-safe and when monomorphization is the better choice. - Distinguishing associated types from generic type parameters and knowing when each applies.
- Using supertraits (
trait B: A) to require that implementing types also satisfy another trait.
How are trait objects different from generic trait bounds?
A generic fn show<T: Display>(item: T) produces a separate compiled copy for each concrete type passed in, so calls are inlined and fast, but binary size grows. A trait object fn show(item: &dyn Display) uses one copy of the function and dispatches at runtime through a vtable stored in the fat pointer, which keeps binaries smaller at the cost of an indirect call and the loss of type information. Object safety rules follow from that vtable: if a method returns Self, takes self by value, or has its own generic type parameters, the compiler cannot build the vtable entry. A trait that requires Self: Sized is excluded for the same reason, so dyn Trait is rejected.
Key facts
- Tarmac has 13 Rust interview questions on this topic, 10 of them on this page, at difficulty 1–5 of 5.
- Tarmac tracked 265 job postings asking for Rust in November 2026.
- Roles asking for Rust advertise a median base salary of £80,000, across 41 job postings as of November 2026.
- Tarmac last reviewed these Rust interview questions on 3 December 2026.
At a glance
| Questions | 10 shown · 13 in the bank |
|---|---|
| Difficulty | 1–5 of 5 |
| Formats | Multiple choice, Ordering, Fill in the blank, Multiple answer, Code output, True / false, Find the bug, Short answer, Flashcard |
What you'll review
- traits
- structs enums
Practice questions
Try one before you open the answer. Pick an option and press Check; it's marked on the spot.
In Rust, which keyword do you use to implement a trait Speak for a struct Dog?#
Options
Show answer
To implement a trait for a type in Rust, write impl Speak for Dog { ... }. The general form is impl Trait for Type. Inside the block you define the trait's required methods. The trait keyword only declares a trait. Rust has no implements keyword.
Rust implements a trait for a type with the syntax impl Trait for Type, so the correct form is impl Speak for Dog { ... }. Writing impl Dog for Speak puts the type and the trait in the wrong positions. implements is Java syntax and is not a Rust keyword. trait Speak for Dog is invalid because the trait keyword only declares a trait. It doesn't implement one.
Put these tokens in order so they form the first line of a valid Rust trait implementation that implements the trait Summary for the struct Article.#
Put these in order
Show answer
A Rust trait implementation is written impl Summary for Article {: the impl keyword, then the trait name, then for, then the type, then an opening brace. The trait always comes before for and the type comes after it. Writing impl Article { with no trait creates an inherent impl, which adds methods to the type itself, not a trait implementation.
Rust's trait implementation syntax is impl TraitName for TypeName { ... }. It starts with the impl keyword. Next comes the trait being implemented, Summary, then the for keyword, then the type that gets the trait, Article. Last, an opening brace starts the block where the trait's methods are defined. Putting the type before the trait, or leaving out for, gives an inherent impl or a syntax error.
To let the compiler auto-generate trait implementations for a struct, you annotate it with #[_____(Clone, Debug)]. With that in place, Debug enables printing the value using the _____ format specifier inside println!.#
Show answer
To let the compiler auto-generate trait implementations for a struct, you annotate it with #[**derive**(Clone, Debug)]. With that in place, Debug enables printing the value using the **{:?}** format specifier inside println!.
#[derive(...)] is the attribute that asks the compiler to auto-implement common traits structurally — Clone, Debug, PartialEq, Hash, Default, and others — saving you the boilerplate of writing each impl by hand. Deriving Debug lets you print a value with the {:?} specifier (or {:#?} for pretty, multi-line output) in println!/format!, which is the standard way to dump a struct for debugging. Note {} (Display) is not auto-derivable — you must implement Display yourself, because the human-facing representation is a deliberate design choice, whereas Debug's programmer-facing one can be generated mechanically.
Consider this Rust trait:#
Options
Pick every one that applies.
Show answer
A Rust trait method with no body is required, so every implementor must define it. A method with a default body can be left out, which uses the default, or overridden. A default method can call the trait's required methods. An impl Trait for Type block may define only items the trait declares, so extra methods go in an inherent impl block.
name is a required method with no default, so every implementor must define it. hello has a default body, so implementors may leave it out and inherit it, or override it. Default methods can call required methods: the actual implementation is filled in for each concrete type. The claim that hello can't call name is therefore false. An impl Trait for Type block may contain only items the trait declares. Any extra method gives error E0407, so you'd put extra methods in an inherent impl MyType block.
What does this Rust program print?#
trait Greet {
fn name(&self) -> String;
fn greet(&self) -> String {
format!("Hello, {}!", self.name())
}
}
struct En;
struct Fr;
impl Greet for En {
fn name(&self) -> String { "Ann".to_string() }
}
impl Greet for Fr {
fn name(&self) -> String { "Luc".to_string() }
fn greet(&self) -> String { format!("Bonjour, {}!", self.name()) }
}
fn main() {
println!("{} {}", En.greet(), Fr.greet());
}Show answer
Hello, Ann! Bonjour, Luc!
En implements only the required method name, so it inherits the trait's default greet. That default calls En's own name and builds "Hello, Ann!". Fr overrides greet with its own version, so its call produces "Bonjour, Luc!". The two strings are printed on one line with a single space between them, which gives Hello, Ann! Bonjour, Luc!.
In current stable Rust, consider a trait like this:#
Options
Show answer
No, Shape is still dyn-compatible. A trait method that returns Self but has a where Self: Sized bound does not stop the trait from being used as a trait object. Rust leaves that method out of the vtable, so Box<dyn Shape> compiles and can call the other methods. Only the Sized-bounded method can't be called through dyn Shape.
The statement is false. Rust's dyn-compatibility (object-safety) rules skip any method that has a where Self: Sized bound. A trait object such as dyn Shape is unsized, so it could never call that method anyway. Because of this, duplicate is left out of the vtable and does not stop the trait from being used as dyn Shape. You can still call area through Box<dyn Shape>. Only duplicate is unavailable on the trait object, while concrete sized types can call it normally. Without the Sized bound, the Self return type would make the trait non-dyn-compatible.
This code fails to compile with error E0038. Which line does rustc flag as the primary error location (the line its main error span points at)?#
trait Shape {
fn area(&self) -> f64;
fn scale<T: Into<f64>>(&self, factor: T) -> f64;
}
struct Square(f64);
impl Shape for Square {
fn area(&self) -> f64 { self.0 * self.0 }
fn scale<T: Into<f64>>(&self, factor: T) -> f64 { self.area() * factor.into() }
}
fn main() {
let shapes: Vec<Box<dyn Shape>> = vec![Box::new(Square(2.0))];
println!("{}", shapes[0].area());
}Show answer
The bug is on line 14.
rustc reports E0038 with its primary span on line 14, where the type dyn Shape is written inside Vec<Box<dyn Shape>>. That is the site where the compiler requires the trait to be dyn compatible (object safe) and rejects it. The trait is not dyn compatible because its generic method scale on line 3 cannot be placed in a vtable with a fixed set of function pointers, so the compiler also adds a note pointing at line 3. The trait declaration and the impl are valid on their own, and the error is only raised where dyn Shape is used, so line 14 is the reported error location. Adding where Self: Sized to scale or changing its parameter to a non-generic type would make line 14 compile.
In Rust, this trait does not compile when used as Box<dyn Shape>:#
Show answer
A trait object works through a vtable that holds one concrete function pointer per method, and the concrete type behind it has been erased. new_default has no self receiver, so a vtable has nothing to dispatch on. It also returns Self, whose size isn't known for dyn Shape. scale is generic over T, so it would need a separate vtable entry for every possible T, which is impossible. It also returns Self by value. The fix is to add a where Self: Sized bound to those methods, e.g. fn new_default() -> Self where Self: Sized;. That removes them from the vtable, so dyn Shape becomes valid and only area can be called through the trait object. Concrete types can still call the excluded methods. Another option is to move those methods into a separate trait.
A trait is dyn-compatible only if every method can be called through a vtable. That rules out methods with no receiver, methods that return Self by value (an unsized type for dyn Trait), and methods with generic type parameters, because they would need endless monomorphized vtable entries. Adding where Self: Sized to those methods removes them from the trait object's interface. The trait stays usable as dyn Shape, and concrete types can still call those methods.
In Rust, a trait with a generic method like fn visit<T>(&self, t: T); normally cannot be used as dyn Trait, because the vtable cannot hold a separate entry for every T. You can keep the trait usable as a trait object by excluding that method from the vtable with the bound fn visit<T>(&self, t: T) where Self: _____;. The method can then be called on concrete types but not through &dyn Trait. The rules that decide whether a trait can be made into a trait object are called object safety, now officially renamed _____ compatibility.#
Show answer
In Rust, a trait with a generic method like fn visit<T>(&self, t: T); normally cannot be used as dyn Trait, because the vtable cannot hold a separate entry for every T. You can keep the trait usable as a trait object by excluding that method from the vtable with the bound fn visit<T>(&self, t: T) where Self: **Sized**;. The method can then be called on concrete types but not through &dyn Trait. The rules that decide whether a trait can be made into a trait object are called object safety, now officially renamed dyn compatibility.
Generic methods need monomorphization, so a vtable cannot hold them. Adding where Self: Sized exempts the method from the dyn-compatibility rules. dyn Trait is unsized, so it does not meet the bound, and the compiler never needs a vtable entry for the method. As a result, the method is unavailable on trait objects but still works on concrete sized types. Rust 1.83 era documentation renamed 'object safety' to 'dyn compatibility'.
In Rust, why does impl<T: Display> MyTrait for T {} together with impl MyTrait for Vec<u8> {} fail to compile in the same crate, even though Vec<u8> does not implement Display today?#
Show answer
The coherence/overlap checker refuses to rely on the negative fact "Vec<u8>: !Display". Vec and Display both live in an upstream crate (std), and std could add impl Display for Vec<u8> later as a non-breaking change. If it did, both impls would apply, so rustc reports E0119, conflicting implementations ("upstream crates may add a new impl of trait"). The fixes are specialization (unstable), a local newtype wrapper, or dropping the blanket impl. Fundamental types such as &T and Box<T> are treated as an exception.
Coherence has to stay sound when dependencies evolve. Upstream crates are allowed to add trait impls for their own types without that counting as a breaking change. So overlap checking treats any impl that upstream could add as possibly existing. That is why a blanket impl conflicts with a concrete impl on a foreign type, even though the two don't overlap at the moment. Types marked #[fundamental] are the exception: for them, adding such an impl would count as a breaking change.
Related interview questions
Job market
See rust salaries and hiring demand from live job postings.
The other 3 questions
This page shows 10 and marks what you pick. That's as far as a page can go. A free account opens the other 3 and keeps every answer. What you miss comes back until it's right: after a day, then at longer gaps.
Free · the whole bank · 100 answers per 30 days, each marked with written feedback