0Pricing
Mojo Academy · Lektion

Kopier- und Verschiebesemantik

Wie Werte dupliziert oder verschoben werden.

Kopier- und Verschiebesemantik 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.

Two Ways to Pass Along

When a value moves around your program, Mojo can either copy it or move it. Knowing which happens helps you reason about cost. 🔁

What a Copy Is

A copy makes a brand-new, independent value. Both the original and the copy live on, each owning its own data.

var a = MyType(1)
var b = a   # copy: a still valid

What a Move Is

A move relocates a value instead of duplicating it. The data is handed over, so the source is no longer used afterward.

Why Moves Are Cheaper

A copy may duplicate heap data, but a move just transfers ownership of what is already there. For big values that saves real work.

Assignment Usually Copies

Plain assignment in Mojo defaults to a copy, so each variable owns its own value. This keeps value semantics safe and predictable.

Triggering a Move

You request a move with the transfer operator, the caret sigil. It tells Mojo to hand the value over rather than duplicate it.

var b = a^   # move a into b

The Source Is Spent

After a move, the source variable is consumed. Mojo will not let you read it again, which prevents using stale or invalid data.

Copies Keep Both Alive

By contrast, after a copy both variables are fully usable. Each one is independent, so editing one never touches the other.

The Compiler Decides Smartly

Mojo can also turn a copy into a move automatically when the source is no longer needed, an optimization that quietly saves work.

Cost Awareness Pays Off

Thinking about copy versus move lets you avoid duplicating large buffers by accident. It is the first step toward efficient Mojo code.

You Stay in Control

Mojo gives you safe defaults but lets you opt into a move when you want speed. You choose the behavior that fits each moment.

Quick Check

Lets test copy versus move.

Recap

A copy makes an independent duplicate, while a move transfers ownership cheaply and spends the source. Now you can predict both. 🎯

Häufig gestellte Fragen

Ist die Lektion „Kopier- und Verschiebesemantik“ kostenlos?

Ja — der vollständige Text von „Kopier- und Verschiebesemantik“ 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 „Kopier- und Verschiebesemantik“?

Wie Werte dupliziert oder verschoben werden. 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 „Kopier- und Verschiebesemantik“?

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. Kopier- und Verschiebesemantik
  2. Die __copyinit__-Methode
  3. Die __moveinit__-Methode
  4. Destruktion und __del__
← Zurück zu Mojo Academy