Taking Ownership with owned
Transfer a value into a function.
Taking Ownership with owned 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.
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. 🎯
Frequently asked questions
Is the “Taking Ownership with owned” lesson free?
Yes — the full text of “Taking Ownership with owned” 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 “Taking Ownership with owned”?
Transfer a value into a function. 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 “Taking Ownership with owned” 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
- Borrowing Arguments Read-Only
- Mutating in Place with inout
- Taking Ownership with owned
- The Transfer Operator