0Pricing
Mojo Academy · Lektion

Mit inout direkt mutieren

Erlauben Sie einer Funktion, den Wert des Aufrufers zu ändern.

Mit inout direkt mutieren ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.

Sometimes You Must Change It

Borrowing only lets you read. But sometimes a function needs to actually modify the caller's value, like bumping a counter. ✏️

Meet inout

The inout convention gives a function mutable access. Changes you make inside are reflected back in the caller's variable.

In and Out

The name says it all: the value goes in, you change it, and the updated value comes back out to the caller. No copy is returned.

Marking a Parameter

You write inout before the parameter name. Now assignments inside the function update the original variable directly.

fn increment(inout n: Int):
    n += 1

Calling It

Pass a variable and watch it change after the call. The function edited the caller's value in place, no return needed.

var count = 5
increment(count)
print(count)  # 6

Must Be Mutable

The argument you pass to an inout parameter must be a mutable var. You cannot hand it a fixed literal or a borrowed value.

Exclusive While Borrowed

While a function holds an inout reference, nothing else may touch that value. This exclusivity rule prevents conflicting edits.

No Hidden Copies

Like borrowing, inout works by reference, so there is no full copy. You get mutation without paying for duplication.

Caller Still Owns It

With inout the caller keeps ownership. The function only borrows mutably for the duration of the call, then hands control back.

When to Choose inout

Use inout when a function's whole job is to update a value the caller will keep using, such as growing a buffer or resetting a field.

Read vs Mutate, Side by Side

So far: borrowed means read-only, inout means read and write. Picking the right one tells readers exactly what a function does.

Quick Check

Let us check what inout enables.

Recap

inout lets a function mutate the caller's value in place, by reference and with no copy. The caller still owns it the whole time. 🎯

Häufig gestellte Fragen

Ist die Lektion „Mit inout direkt mutieren“ kostenlos?

Ja — der vollständige Text von „Mit inout direkt mutieren“ 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 „Mit inout direkt mutieren“?

Erlauben Sie einer Funktion, den Wert des Aufrufers zu ändern. 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 2 von 4.

Wie lange dauert die Lektion „Mit inout direkt mutieren“?

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

  1. Argumente schreibgeschützt ausleihen
  2. Mit inout direkt mutieren
  3. Mit owned Besitz übernehmen
  4. Der Transferoperator
← Zurück zu Mojo Academy