コピーとムーブのセマンティクス
値が複製または移動される仕組みを学びます
「コピーとムーブのセマンティクス」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Two Ways to Pass Along
When a value moves around your program, Mojo can either copy it or move it. Knowing which happens helps you reason about cost. 🔁
What a Copy Is
A copy makes a brand-new, independent value. Both the original and the copy live on, each owning its own data.
var a = MyType(1)
var b = a # copy: a still validWhat a Move Is
A move relocates a value instead of duplicating it. The data is handed over, so the source is no longer used afterward.
Why Moves Are Cheaper
A copy may duplicate heap data, but a move just transfers ownership of what is already there. For big values that saves real work.
Assignment Usually Copies
Plain assignment in Mojo defaults to a copy, so each variable owns its own value. This keeps value semantics safe and predictable.
Triggering a Move
You request a move with the transfer operator, the caret sigil. It tells Mojo to hand the value over rather than duplicate it.
var b = a^ # move a into bThe Source Is Spent
After a move, the source variable is consumed. Mojo will not let you read it again, which prevents using stale or invalid data.
Copies Keep Both Alive
By contrast, after a copy both variables are fully usable. Each one is independent, so editing one never touches the other.
The Compiler Decides Smartly
Mojo can also turn a copy into a move automatically when the source is no longer needed, an optimization that quietly saves work.
Cost Awareness Pays Off
Thinking about copy versus move lets you avoid duplicating large buffers by accident. It is the first step toward efficient Mojo code.
You Stay in Control
Mojo gives you safe defaults but lets you opt into a move when you want speed. You choose the behavior that fits each moment.
Quick Check
Lets test copy versus move.
Recap
A copy makes an independent duplicate, while a move transfers ownership cheaply and spends the source. Now you can predict both. 🎯
よくある質問
「コピーとムーブのセマンティクス」レッスンは無料ですか?
はい。「コピーとムーブのセマンティクス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「コピーとムーブのセマンティクス」で何を学びますか?
値が複製または移動される仕組みを学びます ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「コピーとムーブのセマンティクス」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- コピーとムーブのセマンティクス
- __copyinit__メソッド
- __moveinit__メソッド
- 破棄と__del__