The __moveinit__ Method
Define how your type moves.
The __moveinit__ Method is a free Mojo Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.ptrSteal, 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. 🎯
Frequently asked questions
Is the “The __moveinit__ Method” lesson free?
Yes — the full text of “The __moveinit__ Method” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “The __moveinit__ Method”?
Define how your type moves. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The __moveinit__ Method” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Copy vs Move Semantics
- The __copyinit__ Method
- The __moveinit__ Method
- Destruction and __del__