0Pricing
Mojo Academy · Lezione

L'operatore di trasferimento

Sposti i valori con il simbolo ^.

L'operatore di trasferimento è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Mojo Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Mojo Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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. 🎯

Domande Frequenti

La lezione «L'operatore di trasferimento» è gratuita?

Sì — il testo completo di «L'operatore di trasferimento» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Mojo Academy, passa a CoddyKit PRO. Il corso Mojo Academy include 4 lezioni in totale.

Cosa imparerò in «L'operatore di trasferimento»?

Sposti i valori con il simbolo ^. Eserciti Mojo Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Mojo Academy?

Non è richiesta alcuna esperienza precedente. Mojo Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «L'operatore di trasferimento»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Mojo Academy?

Sì. Ogni lezione Mojo Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Prendere in prestito argomenti in sola lettura
  2. Mutare sul posto con inout
  3. Prendere ownership con owned
  4. L'operatore di trasferimento
← Torna a Mojo Academy