Emprestando argumentos somente para leitura
O padrão borrowed para leituras seguras.
Emprestando argumentos somente para leitura é uma aula grátis de Mojo Academy no CoddyKit. Esta é a aula 1 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.
Why Ownership Matters
When you pass a value to a function, Mojo needs to know who can read or change it. That rule is an ownership convention. 🔑
The Default Is Borrowing
By default an argument is borrowed. The function gets read access to the caller's value but does not take it over.
Read, Don't Modify
A borrowed argument is read-only inside the function. You can look at it freely, but Mojo will not let you change it.
No Copy Needed
Borrowing avoids copying the data. The function shares the caller's value as an immutable reference, which keeps things fast.
Spelling It Out
You can write borrowed before a parameter to be explicit, though it is already the default for an fn function.
fn area(borrowed w: Int, borrowed h: Int) -> Int:
return w * hThe Same Without the Keyword
Because borrowing is the default, you usually skip the keyword. This function reads its argument exactly the same way.
fn area(w: Int, h: Int) -> Int:
return w * hTrying to Mutate Fails
If you assign to a borrowed argument, the compiler stops you. That error is Mojo protecting the caller's value.
Safe to Share
Because the function only reads, you can borrow the same value in many calls at once with no risk of a surprise change.
Great for Big Data
Borrowing shines when a value is large. You read a big struct without paying the cost of a full copy on every call.
Caller Keeps Ownership
After a borrowed call, the original variable is still valid and unchanged. The caller never lost ownership of its value.
The Safe Read Default
Borrowing is Mojo's safe, efficient default: read access, no copy, no mutation. Reach for it whenever a function only needs to inspect data.
Quick Check
Let us test what a borrowed argument allows.
Recap
Borrowing is the default: the function reads the caller's value with no copy and no mutation. It is Mojo's safe, fast way to pass data. 🎯
Perguntas Frequentes
A aula “Emprestando argumentos somente para leitura” é grátis?
Sim — o texto completo de “Emprestando argumentos somente para leitura” é 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 “Emprestando argumentos somente para leitura”?
O padrão borrowed para leituras seguras. 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 1 de 4.
Quanto tempo leva a aula “Emprestando argumentos somente para leitura”?
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
- Emprestando argumentos somente para leitura
- Mutação no local com inout
- Assumindo a propriedade com owned
- O operador de transferência