0Pricing
Mojo Academy · درس

جعل بنية متوافقة مع Trait

نفّذ الدوال التي تتطلبها Trait

جعل بنية متوافقة مع Trait درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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. ✅

الأسئلة الشائعة

هل درس «جعل بنية متوافقة مع Trait» مجاني؟

نعم — نص درس «جعل بنية متوافقة مع Trait» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.

ماذا ستتعلم في «جعل بنية متوافقة مع Trait»؟

نفّذ الدوال التي تتطلبها Trait تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟

لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «جعل بنية متوافقة مع Trait»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟

نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ما هي Trait؟
  2. جعل بنية متوافقة مع Trait
  3. Traits مدمجة مثل Copyable
  4. دوال عامة فوق Traits
← العودة إلى Mojo Academy