ownedによる所有権の取得
値を関数に移します
「ownedによる所有権の取得」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🎯
よくある質問
「ownedによる所有権の取得」レッスンは無料ですか?
はい。「ownedによる所有権の取得」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「ownedによる所有権の取得」で何を学びますか?
値を関数に移します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「ownedによる所有権の取得」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 読み取り専用で引数を借用する
- inoutによるその場での変更
- ownedによる所有権の取得
- Transfer演算子