Argumente schreibgeschützt ausleihen
Der Standard für sicheres Lesen ist borrowed.
Argumente schreibgeschützt ausleihen ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Argumente schreibgeschützt ausleihen“ kostenlos?
Ja — der vollständige Text von „Argumente schreibgeschützt ausleihen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Argumente schreibgeschützt ausleihen“?
Der Standard für sicheres Lesen ist borrowed. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Argumente schreibgeschützt ausleihen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Argumente schreibgeschützt ausleihen
- Mit inout direkt mutieren
- Mit owned Besitz übernehmen
- Der Transferoperator