0Pricing
Mojo Academy · Aula

Adaptando uma struct a um trait

Implemente os métodos exigidos por um trait.

Adaptando uma struct a um trait é uma aula grátis de Mojo Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Mojo Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Mojo Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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: String

Implement 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.name

The 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: String

Behavior 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. ✅

Perguntas Frequentes

A aula “Adaptando uma struct a um trait” é grátis?

Sim — o texto completo de “Adaptando uma struct a um trait” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Mojo Academy, atualize para CoddyKit PRO. O curso de Mojo Academy inclui 4 aulas no total.

O que vou aprender em “Adaptando uma struct a um trait”?

Implemente os métodos exigidos por um trait. Você pratica Mojo Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Mojo Academy?

Nenhuma experiência prévia é necessária. Mojo Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Adaptando uma struct a um trait”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Mojo Academy?

Sim. Cada aula de Mojo Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. O que é um trait?
  2. Adaptando uma struct a um trait
  3. Traits integrados, como Copyable
  4. Funções genéricas sobre traits
← Voltar para Mojo Academy