0Pricing
Mojo Academy · レッスン

Transfer演算子

^記号で値を移動します

「Transfer演算子」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Move, Don't Copy

Passing to an owned parameter usually makes a copy. But if you are done with the original, copying is wasteful. You can move it instead. 🚚

Meet the Transfer Operator

Mojo's transfer operator is the caret ^ sigil. You place it after a value to hand its ownership over instead of duplicating it.

Using It in a Call

Add ^ when calling a function with an owned parameter. The value transfers in, and no copy is made.

var msg = String("hi")
consume(msg^)

The Original Is Gone

After a transfer, the source variable is no longer valid. Mojo ends its lifetime, so you cannot accidentally use the moved value.

The Compiler Enforces It

Try to read a variable after moving it and the compiler rejects your code. This check stops use-after-move bugs before they run.

Why Moving Is Faster

A move just transfers ownership of the existing data. There is no deep copy, which matters a lot for big strings, lists, or buffers.

Copy or Move, Your Choice

Without ^ Mojo copies into an owned parameter; with ^ it moves. You decide based on whether you still need the original.

Transfer on Assignment

The caret also works between variables. Writing one var from another with ^ moves the value rather than copying it across.

var a = String("data")
var b = a^  # a is now invalid

Pairs With owned

The transfer operator is the natural partner of owned parameters. Together they express giving a value away cleanly and cheaply.

Safety Plus Speed

Moves give you C-like efficiency while the compiler guarantees you never touch a value after handing it off. That is safe and fast.

The Full Ownership Picture

borrowed, inout, owned, and the ^ transfer together let you control exactly how data flows through your program, with no surprises.

Quick Check

Let us check what the ^ operator does to the source.

Recap

The ^ transfer operator moves a value instead of copying it, ending the source's life. Paired with owned, it makes giving data away fast and safe. 🎯

よくある質問

「Transfer演算子」レッスンは無料ですか?

はい。「Transfer演算子」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「Transfer演算子」で何を学びますか?

^記号で値を移動します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「Transfer演算子」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 読み取り専用で引数を借用する
  2. inoutによるその場での変更
  3. ownedによる所有権の取得
  4. Transfer演算子
← Mojo Academyに戻る