0Pricing
Mojo Academy · Lesson

Copy vs Move Semantics

How values are duplicated or relocated.

Copy vs Move Semantics is a free Mojo Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Copy vs Move Semantics” lesson free?

Yes — the full text of “Copy vs Move Semantics” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Copy vs Move Semantics”?

How values are duplicated or relocated. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Copy vs Move Semantics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Copy vs Move Semantics
  2. The __copyinit__ Method
  3. The __moveinit__ Method
  4. Destruction and __del__
← Back to Mojo Academy