0Pricing
Mojo Academy · درس

دالة __moveinit__

حدّد كيفية نقل نوعك

دالة __moveinit__ درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Defining How to Move

To move a value, Mojo calls a special method named __moveinit__. It builds a new instance by taking over an old one. 🚚

It Runs on Transfer

When you use the caret transfer operator, __moveinit__ fires. It relocates the value instead of duplicating its data.

var b = a^   # calls __moveinit__

The Method Signature

It receives the new instance and the owned source value. Because the source is owned, you may consume it directly.

fn __moveinit__(out self, owned existing: Self):
    self.ptr = existing.ptr

Steal, Do Not Copy

The trick is to transfer resources like a heap pointer from the source to the new instance, rather than allocating again.

No Double Free

After the move, the source must not free the data it gave away. Mojo handles this so the buffer is owned by exactly one instance.

Cheap for Big Values

Because __moveinit__ shuffles ownership instead of cloning bytes, moving a huge buffer costs almost nothing. That is the speed win.

Source Becomes Invalid

Once moved from, the source is consumed. Mojo stops you from reading it, guaranteeing you never touch relocated data.

Often Derived for You

Like copying, you usually do not write this. Conforming to Movable lets Mojo synthesize a field-wise __moveinit__ for you.

When to Write It Yourself

Customize __moveinit__ when your type owns a resource that needs explicit hand-off, such as a manually managed pointer.

Pairs with Copy

A type can define both copy and move. Mojo then picks the cheaper one, choosing a move whenever the source is no longer needed.

Enables Cheap Ownership

__moveinit__ is what makes ownership transfer fast and safe. It lets values flow through your program without wasteful duplication.

Quick Check

Lets check the move initializer.

Recap

__moveinit__ defines how your type moves, handing resources to a new instance cheaply and consuming the source. 🎯

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

هل درس «دالة __moveinit__» مجاني؟

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

ماذا ستتعلم في «دالة __moveinit__»؟

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

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

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

كم من الوقت يستغرق درس «دالة __moveinit__»؟

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

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

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

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

  1. دلالات النسخ مقابل النقل
  2. دالة __copyinit__
  3. دالة __moveinit__
  4. التدمير و__del__
← العودة إلى Mojo Academy