tezvyn:

What is the difference between defining a Rust trait and implementing it?

AI-drafted, machine-checkedSource: doc.rust-lang.orgbeginner
WHAT IT TESTS

Knowledge of declaration versus implementation in Rust traits.

ANSWER OUTLINE

Define with trait and signatures; implement with impl Trait for Type and concrete bodies.

RED FLAG

Mixing up traits and structs or omitting for.

WHAT THIS TESTS: The interviewer is checking if you treat trait definition as a contract declaration and trait implementation as the concrete fulfillment of that contract for a specific type. Defining a trait establishes an abstract interface: a set of method signatures without bodies that describe what behavior exists. Implementing a trait binds those signatures to actual logic for a concrete struct or enum. This distinction mirrors the difference between declaring an API and writing the code that backs it, and it is fundamental to how Rust achieves polymorphism without inheritance.

A GOOD ANSWER COVERS: Four things in order. First, the trait keyword is used to declare the interface, containing method signatures that end with semicolons and have no bodies, effectively saying what capabilities a type must have. Second, the impl TraitName for Type syntax is used to attach the behavior to a specific struct or enum, filling in curly braces with concrete logic that is unique to that type. Third, the compiler enforces that every signature in the trait definition matches exactly what appears in the impl block in terms of parameters, return type, and receiver. Fourth, users must bring both the type and the trait into scope to call trait methods, because Rust requires explicit awareness of which interfaces are being used.

COMMON WRONG ANSWERS: Saying that traits are like structs or that impl alone defines the interface. Another red flag is forgetting the for keyword and writing impl Dog instead of impl Speak for Dog, which actually defines inherent methods rather than a trait implementation. Some candidates also confuse trait objects or dyn with basic trait definition and implementation, or they claim that traits contain fields like structs do.

LIKELY FOLLOW-UPS: The interviewer might ask how trait bounds work on generic functions, when to use default method bodies in a trait definition, or how the orphan rule restricts implementing foreign traits on foreign types. They may also ask about the difference between trait implementations and inherent impl blocks, or when to choose static dispatch via generics over dynamic dispatch via trait objects.

ONE CONCRETE EXAMPLE: Define a Speak trait with a single method signature fn speak(&self) -> String. Then create a Dog struct with a name field of type String. Implement Speak for Dog by returning a format string that says the dog's name followed by says woof. In main, instantiate Dog, bring Speak into scope with a use statement, and call speak on the instance to get the formatted output.

Read the original → doc.rust-lang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.