0Pricing
Mojo Academy · Aula

O operador de transferência

Mova valores com o símbolo ^.

O operador de transferência é uma aula grátis de Mojo Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Mojo Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Mojo Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

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

Perguntas Frequentes

A aula “O operador de transferência” é grátis?

Sim — o texto completo de “O operador de transferência” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Mojo Academy, atualize para CoddyKit PRO. O curso de Mojo Academy inclui 4 aulas no total.

O que vou aprender em “O operador de transferência”?

Mova valores com o símbolo ^. Você pratica Mojo Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Mojo Academy?

Nenhuma experiência prévia é necessária. Mojo Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.

Quanto tempo leva a aula “O operador de transferência”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Mojo Academy?

Sim. Cada aula de Mojo Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Emprestando argumentos somente para leitura
  2. Mutação no local com inout
  3. Assumindo a propriedade com owned
  4. O operador de transferência
← Voltar para Mojo Academy