Conforming a Struct to a Trait
Implement the methods a trait demands.
Conforming a Struct to a Trait is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Signing the Contract
A trait only describes behavior. To use it, a struct must conform by actually implementing the required methods. ✍️
Naming the Trait
You declare conformance by listing the trait in parentheses after the struct name, just like a parameter list.
struct Dog(Greetable):
var name: StringImplement Every Method
Conforming means you must implement each method the trait requires, with a matching name and signature.
Matching the Signature
Your method has to match the trait's signature: same arguments and return type. A mismatch breaks conformance.
fn greet(self) -> String:
return "Woof from " + self.nameThe Compiler Verifies It
When you claim a trait, Mojo checks at compile time that every required method is present and correct.
Missing a Method Fails
Forget a required method and the build fails with a clear error. The contract is enforced, not just suggested.
Conform to Many Traits
A struct can satisfy several traits at once by listing them all, gaining each capability it implements.
struct Dog(Greetable, Stringable):
var name: StringBehavior Stays Yours
The trait says which methods exist, but your struct decides how they work. A Cat and Dog can greet differently.
Extra Methods Are Fine
A struct may add its own methods beyond the trait. Conformance only requires the contract be met, not limited.
Now It Qualifies
Once a struct conforms, it can be passed anywhere that trait is expected. That is the real payoff coming next.
A Concrete Example
Here a Dog conforms to Greetable and supplies its own greeting, fully satisfying the trait.
struct Dog(Greetable):
var name: String
fn greet(self) -> String:
return "Woof!"Quick Check
How does a struct promise to follow a trait?
Recap
To conform, list the trait after the struct name and implement its methods with matching signatures. The compiler checks it. ✅
Frequently asked questions
Is the “Conforming a Struct to a Trait” lesson free?
Yes — the full text of “Conforming a Struct to a Trait” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Conforming a Struct to a Trait”?
Implement the methods a trait demands. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Conforming a Struct to a Trait” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Is a Trait?
- Conforming a Struct to a Trait
- Built-in Traits Like Copyable
- Generic Functions over Traits