読み取り専用で引数を借用する
安全な読み取りのためのデフォルトであるborrowedを学びます
「読み取り専用で引数を借用する」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Why Ownership Matters
When you pass a value to a function, Mojo needs to know who can read or change it. That rule is an ownership convention. 🔑
The Default Is Borrowing
By default an argument is borrowed. The function gets read access to the caller's value but does not take it over.
Read, Don't Modify
A borrowed argument is read-only inside the function. You can look at it freely, but Mojo will not let you change it.
No Copy Needed
Borrowing avoids copying the data. The function shares the caller's value as an immutable reference, which keeps things fast.
Spelling It Out
You can write borrowed before a parameter to be explicit, though it is already the default for an fn function.
fn area(borrowed w: Int, borrowed h: Int) -> Int:
return w * hThe Same Without the Keyword
Because borrowing is the default, you usually skip the keyword. This function reads its argument exactly the same way.
fn area(w: Int, h: Int) -> Int:
return w * hTrying to Mutate Fails
If you assign to a borrowed argument, the compiler stops you. That error is Mojo protecting the caller's value.
Safe to Share
Because the function only reads, you can borrow the same value in many calls at once with no risk of a surprise change.
Great for Big Data
Borrowing shines when a value is large. You read a big struct without paying the cost of a full copy on every call.
Caller Keeps Ownership
After a borrowed call, the original variable is still valid and unchanged. The caller never lost ownership of its value.
The Safe Read Default
Borrowing is Mojo's safe, efficient default: read access, no copy, no mutation. Reach for it whenever a function only needs to inspect data.
Quick Check
Let us test what a borrowed argument allows.
Recap
Borrowing is the default: the function reads the caller's value with no copy and no mutation. It is Mojo's safe, fast way to pass data. 🎯
よくある質問
「読み取り専用で引数を借用する」レッスンは無料ですか?
はい。「読み取り専用で引数を借用する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「読み取り専用で引数を借用する」で何を学びますか?
安全な読み取りのためのデフォルトであるborrowedを学びます ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「読み取り専用で引数を借用する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 読み取り専用で引数を借用する
- inoutによるその場での変更
- ownedによる所有権の取得
- Transfer演算子