0Pricing
Mojo Academy · Lección

El método __moveinit__

Defina cómo se mueve su tipo.

El método __moveinit__ es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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. 🎯

Preguntas frecuentes

¿La lección «El método __moveinit__» es gratis?

Sí — el texto completo de «El método __moveinit__» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «El método __moveinit__»?

Defina cómo se mueve su tipo. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Mojo Academy?

No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.

¿Cuánto tiempo toma la lección «El método __moveinit__»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?

Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Semántica de copia frente a movimiento
  2. El método __copyinit__
  3. El método __moveinit__
  4. Destrucción y __del__
← Volver a Mojo Academy