0Pricing
Mojo Academy · レッスン

StructをTraitに準拠させる

Traitが要求するメソッドを実装します

「StructをTraitに準拠させる」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「StructをTraitに準拠させる」レッスンは無料ですか?

はい。「StructをTraitに準拠させる」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「StructをTraitに準拠させる」で何を学びますか?

Traitが要求するメソッドを実装します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「StructをTraitに準拠させる」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Traitとは
  2. StructをTraitに準拠させる
  3. Copyableなどの組み込みTrait
  4. Traitに対するジェネリック関数
← Mojo Academyに戻る