Traits & Generics Questions with Answers

18 practice questions on Traits & Generics (Rust) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

Which trait controls how a type is printed with {} in println!?

  1. Debug
  2. Display
  3. Default
  4. ToOwned

Answer: B. Display

Explanation

Debug is used by {:?}; Display must be implemented by hand for {}.

Step-by-step solution

  1. The {} placeholder in println uses the Display trait.
  2. The {:?} placeholder uses the Debug trait.
  3. So option B, Display, is the correct answer.

Why the other options are wrong

  • A. Debug controls {:?}, not {}.
  • C. Default gives a default value, not text.
  • D. ToOwned converts a borrowed value into an owned one.

Tip: Implement Display by hand for {}; derive Debug for {:?}.

Question 2Easy

Which trait lets a type be compared with ==?

  1. Clone
  2. PartialEq
  3. Iterator
  4. Display

Answer: B. PartialEq

Explanation

#[derive(PartialEq)] generates field-by-field equality.

Step-by-step solution

  1. The == operator uses the PartialEq trait.
  2. The derive(PartialEq) attribute generates equality comparing each field.
  3. So option B is the correct answer.

Why the other options are wrong

  • A. Clone makes a copy of a value; it does not compare values.
  • C. Iterator is for stepping through items, not comparing.
  • D. Display is for printing, not for ==.

Tip: Derive PartialEq (and Eq when it applies) to compare your types with ==.

Question 3Easy

What is a trait in Rust?

  1. A set of methods a type can implement, like an interface
  2. A macro that copies code from one type into another at compile time
  3. A memory allocator
  4. A struct field

Answer: A. A set of methods a type can implement, like an interface

Explanation

Traits define shared behaviour; types opt in with impl Trait for Type.

Step-by-step solution

  1. A trait lists methods that types can implement.
  2. A type opts in with impl Trait for Type, much like an interface.
  3. So option A is the correct answer.

Why the other options are wrong

  • B. Copying code between types is not what a trait does; that is not a macro concept.
  • C. A trait is not a memory allocator.
  • D. A struct field stores data; a trait defines behaviour.

Tip: Think of a trait as a named set of behaviour that types promise to provide.

Question 4Easy

What does #[derive(Debug)] provide?

  1. Runtime logging of every function call
  2. An automatic Debug implementation, enabling {:?} formatting
  3. A debugger
  4. A Display implementation that prints the struct with {} formatting

Answer: B. An automatic Debug implementation, enabling {:?} formatting

Explanation

Derive macros generate standard trait impls such as Debug, Clone, PartialEq.

Step-by-step solution

  1. The derive(Debug) attribute asks the compiler to generate a Debug implementation.
  2. That implementation lets the type be printed with {:?}.
  3. So option B is the correct answer.

Why the other options are wrong

  • A. Derive Debug does not log function calls at runtime.
  • C. A debugger is a separate tool, not a derive.
  • D. Display is a different trait, and Debug does not give it.

Tip: Derive Debug on most structs so they can be printed while developing.

Question 5Medium

What are default methods in a trait?

  1. Methods that cannot be overridden
  2. Methods every type gets automatically
  3. Method bodies in the trait that implementors can use or override
  4. Static methods that are called once when the program starts

Answer: C. Method bodies in the trait that implementors can use or override

Explanation

Implementors only need to provide the required methods.

Step-by-step solution

  1. A trait can give a method a default body.
  2. Implementors use that body or replace it with their own.
  3. So option C is the correct answer.

Why the other options are wrong

  • A. Default methods can be overridden by implementors.
  • B. A default method is only given to types that implement the trait.
  • D. Default methods are ordinary methods, not one-time static code.

Tip: Implementors need only write the required methods; the rest can use defaults.

Question 6Medium

What does returning impl Iterator<Item = u32> from a function mean?

  1. It returns a boxed trait object
  2. It returns a Vec
  3. It returns some concrete iterator type without naming it
  4. It returns a different iterator type on each call, chosen at runtime

Answer: C. It returns some concrete iterator type without naming it

Explanation

The concrete type is hidden but fixed, keeping static dispatch.

Step-by-step solution

  1. Impl Iterator Item = u32 as a return type hides the concrete iterator type.
  2. The type is still one fixed type chosen by the function body.
  3. So option C is the correct answer.

Why the other options are wrong

  • A. This is dyn Iterator in a Box, not impl Trait.
  • B. The function returns an iterator, not a Vec.
  • D. The concrete type is fixed at compile time, not chosen at runtime.

Tip: Use impl Trait to return an iterator without naming its type.

Question 7Medium

What does fn print_all<T: Display>(items: &[T]) mean?

  1. items must be strings
  2. T must be exactly the Display type
  3. A generic function for any T that implements Display
  4. The function is only compiled once and uses dynamic dispatch for every call

Answer: C. A generic function for any T that implements Display

Explanation

The trait bound restricts T to types that can be formatted with {}.

Step-by-step solution

  1. T is a type parameter, and Display is a trait bound on it.
  2. The bound means any type T that implements Display can be used.
  3. So option C is the correct answer.

Why the other options are wrong

  • A. The items do not have to be strings; any Display type works.
  • B. T is not fixed to one type; it can be any type that implements Display.
  • D. Generics are compiled for each concrete type, not dispatched dynamically.

Tip: A trait bound restricts a generic to types with that behaviour.

Question 8Medium

Why is Box<dyn Shape> used instead of dyn Shape when storing mixed shapes in a Vec?

  1. dyn Shape is unsized, so it needs a pointer
  2. Box makes the method calls faster
  3. dyn types cannot have methods
  4. Vec only stores heap types

Answer: A. dyn Shape is unsized, so it needs a pointer

Explanation

Different implementors have different sizes, so a trait object must be behind a pointer such as Box or &.

Step-by-step solution

  1. Implementors differ in size.
  2. Vec needs sized elements.
  3. Box provides a fixed-size pointer.

Why the other options are wrong

  • B. dyn calls go through a vtable, which is slower, not faster.
  • C. Trait objects do have methods.
  • D. Vec can store any sized type.

Question 9Hard

What is an associated type, such as type Item in Iterator?

  1. A type placeholder each implementation fixes once
  2. A type alias for i32
  3. A generic parameter chosen by the caller each time
  4. A field that every struct implementing the trait must contain

Answer: A. A type placeholder each implementation fixes once

Explanation

Each Iterator implementation names one Item type, avoiding repeated generic parameters.

Step-by-step solution

  1. An associated type, such as type Item in Iterator, is a placeholder type.
  2. Each implementation of the trait sets it once.
  3. So option A is the correct answer.

Why the other options are wrong

  • B. An associated type is chosen by each implementation, not fixed to i32.
  • C. The caller does not choose it each time; the implementation does.
  • D. It is a type in the trait, not a struct field.

Tip: Use an associated type when each implementor should fix exactly one type.

Question 10Hard

dyn Trait (e.g. Box<dyn Shape>) vs generics:

  1. dyn Trait is resolved at compile time
  2. They are identical, except that dyn Trait can only be used with structs and never with enums
  3. Generics need a Box
  4. dyn Trait uses dynamic dispatch through a vtable; generics use static dispatch

Answer: D. dyn Trait uses dynamic dispatch through a vtable; generics use static dispatch

Explanation

Trait objects allow mixed types in one collection at a small runtime cost.

Step-by-step solution

  1. dyn Trait uses a vtable at runtime.
  2. Generics are monomorphised at compile time.
  3. So dyn is dynamic dispatch, generics static.

Why the other options are wrong

  • A. dyn is resolved at runtime.
  • B. dyn Trait works for any type implementing the trait.
  • C. Generics do not need Box.

Practise all 18 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Rust topics · All topics