skip to content

Traits and Generics

Traits are Rust's interfaces and generics are checked against them, with static or dynamic dispatch as a choice. Interviewers test bounds, trait objects, coherence and impl Trait.

on this pageshow

explore

questions

page 2 of 2

In Rust, why can't an `Iterator` yield overlapping `&mut` windows into its own buffer, and how does a generic associated type `type Item<'a>` fix it?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

Iterator's Item is fixed per impl and cannot name the lifetime of next's &mut self borrow, so items may outlive the call and overlapping &mut windows would alias. A generic associated type ties each item to that borrow.

open as a page

After a dependency update, a Rust call `email.retry("disk full", 3)` that compiled before now fails with E0034 — what changed, and how do you fix it?

level: seniorimportance: nice to knowfreq 22%

basics

~20 s

A trait that is in scope at the call site gained a retry method, typically a new default method upstream, so two trait methods now match. Disambiguate with RetryExt::retry(&email, ...) or Notifier::retry(&email, ...), or rename your own trait's method.

open as a page

In Rust, what does `fn skus(&self) -> impl Iterator<Item = u32>` in a trait declaration mean for implementors, generic callers and `dyn` use of that trait?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

Since Rust 1.75, impl Trait in a trait method's return is an anonymous associated type: each impl picks its own hidden type, generic callers see only the declared bounds, and the method blocks dyn use (E0038).

open as a page

On stable Rust, why do `struct A<const X: f32>` and a return type `[u8; N + 1]` using a const parameter `N` both fail to compile?

level: seniorimportance: nice to knowfreq 18%

basics

~20 s

Stable const parameters may only be integers, bool or char, so f32 is forbidden as a const parameter type. Inside a type a const parameter must stand alone, so rustc rejects the arithmetic in N + 1.

open as a page

In a Rust library's typestate builder, why seal the `State` trait that bounds `RequestBuilder<S: State>`, and how does the sealing work?

level: seniorimportance: nice to knowfreq 14%

basics

~20 s

Give State a supertrait declared pub inside a private module and implement it only for NoUrl and HasUrl. Other crates can still use State in bounds but cannot implement it (E0277), so the builder's set of states stays closed.

open as a page

Your team plans third-party Rust plugins as dynamic libraries whose entry point returns `Box<dyn Plugin>`: what does that boundary require, and what would you choose instead?

level: principalimportance: nice to knowfreq 22%

basics

~20 s

Rust promises no stable layout for repr(Rust) types or trait-object vtables, so a Box<dyn Plugin> crossing a separately built library needs, at minimum, the same compiler and trait crate on both sides. A C-ABI function table, WebAssembly or a process are sturdier.

open as a page

showing 31–36 of 36