0Pricing
Mojo Academy · Lezione

Prendere in prestito argomenti in sola lettura

Il comportamento borrowed predefinito per letture sicure.

Prendere in prestito argomenti in sola lettura è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 1 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.

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 * h

The 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 * h

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

Domande Frequenti

La lezione «Prendere in prestito argomenti in sola lettura» è gratuita?

Sì — il testo completo di «Prendere in prestito argomenti in sola lettura» è 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 «Prendere in prestito argomenti in sola lettura»?

Il comportamento borrowed predefinito per letture sicure. 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 1 di 4.

Quanto tempo richiede la lezione «Prendere in prestito argomenti in sola lettura»?

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