0Pricing
Mojo Academy · Lesson

What Is a Trait?

A contract of required methods.

What Is a Trait? is a free Mojo Academy lesson on CoddyKit — lesson 1 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.

Behavior You Can Share

Sometimes many types need the same ability, like being printable. A trait lets you describe that shared behavior once. 🤝

A Contract of Methods

Think of a trait as a contract: it lists the methods a type must provide, without saying how they work.

Like an Interface

If you have met interfaces in other languages, a trait is the same idea. It names the required methods and nothing more.

Declaring a Trait

You declare a trait with the trait keyword, then list method signatures inside it, much like a struct.

trait Greetable:
    fn greet(self) -> String:
        ...

The Three Dots Mean Empty

The ... body marks a method as required but unimplemented. The trait promises the method exists, not what it does.

No Code in the Trait

A trait usually holds only signatures. The actual logic lives in each type that agrees to follow the contract.

Why Traits Help

Traits let you write code against a capability instead of a specific type. Any conforming type just works.

Many Types, One Promise

A Dog and a Robot can both promise to greet. They share the trait while keeping totally different internals.

Traits vs Inheritance

Structs have no class-style inheritance, so traits are how Mojo shares behavior. They describe what, not a base type.

Compile-Time Checked

Mojo verifies trait conformance at compile time. If a type misses a required method, your build fails early.

The Building Block

Traits are the foundation for generic Mojo code. Master this contract idea and reusable, type-safe functions come next. 🧱

Quick Check

Let us pin down what a trait really is.

Recap

A trait is a contract of required methods. It describes shared behavior so many types can be used the same way. 🎯

Frequently asked questions

Is the “What Is a Trait?” lesson free?

Yes — the full text of “What Is 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 “What Is a Trait?”?

A contract of required methods. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “What Is 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

  1. What Is a Trait?
  2. Conforming a Struct to a Trait
  3. Built-in Traits Like Copyable
  4. Generic Functions over Traits
← Back to Mojo Academy