Tomar la propiedad con owned
Transfiera un valor a una función.
Tomar la propiedad con owned 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.
A Third Way to Pass
You have borrowed for reading and inout for editing. The third convention lets a function take over a value entirely. 📦
Meet owned
An owned argument transfers ownership into the function. The function now holds the value as its very own to use or store.
Marking the Parameter
You write owned before the parameter name. Inside, the function has full control: it can mutate, store, or consume the value.
fn consume(owned name: String):
print(name)The Function Becomes the Owner
With owned, the value's lifetime moves into the function. When the function ends, it is responsible for destroying the value.
Why Take Ownership?
Use owned when a function needs to keep the value, like storing it in a struct or a collection, rather than just reading it briefly.
Owned Means Mutable Too
Since the function owns the value, it may freely change it. There is no caller to protect, because the value now belongs to the function.
How the Value Arrives
By default Mojo gives an owned parameter a fresh copy, so the caller's original stays intact unless you ask to move it.
Avoiding the Copy
If you no longer need the original, you can hand the value over instead of copying it. That transfer is the topic of the next lesson.
Clear Intent
Choosing owned tells readers this function will keep the value. The signature documents the data's journey at a glance.
The Three Conventions
Now you have all three: borrowed reads, inout mutates in place, and owned takes the value over. Each maps to a clear intent.
Pick by Responsibility
Ask who should be responsible for the value after the call. If the answer is the function, reach for owned.
Quick Check
Let us check what owned means for a function.
Recap
owned hands a value's ownership to the function, which can keep, change, or destroy it. Use it when the function must own the data. 🎯
Preguntas frecuentes
¿La lección «Tomar la propiedad con owned» es gratis?
Sí — el texto completo de «Tomar la propiedad con owned» 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 «Tomar la propiedad con owned»?
Transfiera un valor a una función. 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 «Tomar la propiedad con owned»?
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