0Pricing
Mojo Academy · Aula

Assumindo a propriedade com owned

Transfira um valor para uma função.

Assumindo a propriedade com owned é uma aula grátis de Mojo Academy no CoddyKit. Esta é a aula 3 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.

A Third Way to Pass

You have borrowed for reading and inout for editing. The third convention lets a function take over a value entirely. 📦

Meet owned

An owned argument transfers ownership into the function. The function now holds the value as its very own to use or store.

Marking the Parameter

You write owned before the parameter name. Inside, the function has full control: it can mutate, store, or consume the value.

fn consume(owned name: String):
    print(name)

The Function Becomes the Owner

With owned, the value's lifetime moves into the function. When the function ends, it is responsible for destroying the value.

Why Take Ownership?

Use owned when a function needs to keep the value, like storing it in a struct or a collection, rather than just reading it briefly.

Owned Means Mutable Too

Since the function owns the value, it may freely change it. There is no caller to protect, because the value now belongs to the function.

How the Value Arrives

By default Mojo gives an owned parameter a fresh copy, so the caller's original stays intact unless you ask to move it.

Avoiding the Copy

If you no longer need the original, you can hand the value over instead of copying it. That transfer is the topic of the next lesson.

Clear Intent

Choosing owned tells readers this function will keep the value. The signature documents the data's journey at a glance.

The Three Conventions

Now you have all three: borrowed reads, inout mutates in place, and owned takes the value over. Each maps to a clear intent.

Pick by Responsibility

Ask who should be responsible for the value after the call. If the answer is the function, reach for owned.

Quick Check

Let us check what owned means for a function.

Recap

owned hands a value's ownership to the function, which can keep, change, or destroy it. Use it when the function must own the data. 🎯

Perguntas Frequentes

A aula “Assumindo a propriedade com owned” é grátis?

Sim — o texto completo de “Assumindo a propriedade com owned” é 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 “Assumindo a propriedade com owned”?

Transfira um valor para uma função. 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 3 de 4.

Quanto tempo leva a aula “Assumindo a propriedade com owned”?

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