0Pricing
Mojo Academy · บทเรียน

เมธอด __moveinit__

กำหนดวิธีที่ชนิดข้อมูลของคุณย้ายตัวเอง

เมธอด __moveinit__ เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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__” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เมธอด __moveinit__”

กำหนดวิธีที่ชนิดข้อมูลของคุณย้ายตัวเอง คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “เมธอด __moveinit__” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม

ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ซีแมนติกส์ของการคัดลอกเทียบกับการย้าย
  2. เมธอด __copyinit__
  3. เมธอด __moveinit__
  4. การทำลายและ __del__
← กลับไปที่ Mojo Academy