0Pricing
Mojo Academy · Lección

¿Qué es un trait?

Un contrato de métodos obligatorios.

¿Qué es un trait? es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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. 🎯

Preguntas frecuentes

¿La lección «¿Qué es un trait?» es gratis?

Sí — el texto completo de «¿Qué es un trait?» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «¿Qué es un trait?»?

Un contrato de métodos obligatorios. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Mojo Academy?

No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «¿Qué es un trait?»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?

Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. ¿Qué es un trait?
  2. Hacer que una struct cumpla un trait
  3. Traits integrados como Copyable
  4. Funciones genéricas sobre traits
← Volver a Mojo Academy