El operador de transferencia
Mueva valores con el símbolo ^.
El operador de transferencia es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 4 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.
Move, Don't Copy
Passing to an owned parameter usually makes a copy. But if you are done with the original, copying is wasteful. You can move it instead. 🚚
Meet the Transfer Operator
Mojo's transfer operator is the caret ^ sigil. You place it after a value to hand its ownership over instead of duplicating it.
Using It in a Call
Add ^ when calling a function with an owned parameter. The value transfers in, and no copy is made.
var msg = String("hi")
consume(msg^)The Original Is Gone
After a transfer, the source variable is no longer valid. Mojo ends its lifetime, so you cannot accidentally use the moved value.
The Compiler Enforces It
Try to read a variable after moving it and the compiler rejects your code. This check stops use-after-move bugs before they run.
Why Moving Is Faster
A move just transfers ownership of the existing data. There is no deep copy, which matters a lot for big strings, lists, or buffers.
Copy or Move, Your Choice
Without ^ Mojo copies into an owned parameter; with ^ it moves. You decide based on whether you still need the original.
Transfer on Assignment
The caret also works between variables. Writing one var from another with ^ moves the value rather than copying it across.
var a = String("data")
var b = a^ # a is now invalidPairs With owned
The transfer operator is the natural partner of owned parameters. Together they express giving a value away cleanly and cheaply.
Safety Plus Speed
Moves give you C-like efficiency while the compiler guarantees you never touch a value after handing it off. That is safe and fast.
The Full Ownership Picture
borrowed, inout, owned, and the ^ transfer together let you control exactly how data flows through your program, with no surprises.
Quick Check
Let us check what the ^ operator does to the source.
Recap
The ^ transfer operator moves a value instead of copying it, ending the source's life. Paired with owned, it makes giving data away fast and safe. 🎯
Preguntas frecuentes
¿La lección «El operador de transferencia» es gratis?
Sí — el texto completo de «El operador de transferencia» 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 operador de transferencia»?
Mueva valores con el símbolo ^. 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 4 de 4.
¿Cuánto tiempo toma la lección «El operador de transferencia»?
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
- Tomar argumentos prestados en modo de solo lectura
- Mutar en el sitio con inout
- Tomar la propiedad con owned
- El operador de transferencia