Il metodo __moveinit__
Definisca il modo in cui il tipo viene spostato.
Il metodo __moveinit__ è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Mojo Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Mojo Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🎯
Domande Frequenti
La lezione «Il metodo __moveinit__» è gratuita?
Sì — il testo completo di «Il metodo __moveinit__» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Mojo Academy, passa a CoddyKit PRO. Il corso Mojo Academy include 4 lezioni in totale.
Cosa imparerò in «Il metodo __moveinit__»?
Definisca il modo in cui il tipo viene spostato. Eserciti Mojo Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Mojo Academy?
Non è richiesta alcuna esperienza precedente. Mojo Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Il metodo __moveinit__»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Mojo Academy?
Sì. Ogni lezione Mojo Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Semantica di copia e spostamento
- Il metodo __copyinit__
- Il metodo __moveinit__
- Distruzione e __del__