0Pricing
Mojo Academy · レッスン

ミューテーションの基本

他のコードを壊さずにデータを変更します

「ミューテーションの基本」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Changing Data On Purpose

Independent copies are great, but real programs must mutate data: update a score, grow a list. Let us do it safely. ✏️

Mutating Your Own var

The simplest mutation is reassigning a var you own. Since it is your copy, the change affects only your value.

var score = 10
score = score + 5

Local Mutation Is Free of Side Effects

Editing a local var cannot leak outward. Other variables keep their values, so the change has no surprise side effects.

var a = 1
var b = a
a = 100

Functions Get Copies By Default

Pass a value to a function and it edits a copy. Your original outside the function is left untouched.

fn bump(n: Int) -> Int:
    return n + 1

But Sometimes You Want Real Change

What if a function should update the caller's value in place? Mojo lets you opt in with the inout convention.

inout Means Mutate In Place

Mark an argument inout and the function works on the caller's actual value, so its edits truly stick.

fn double(inout n: Int):
    n = n * 2

Mutation Is Explicit, Not Accidental

Notice you had to write inout on purpose. Mojo never mutates the caller behind your back; you always opt in.

Reading the Signature Tells You

The function signature reveals intent. See inout and you know your value may change; otherwise expect a safe copy.

Calling an inout Function

Call it with your var and watch the value update right where it lives. The function reached your original, by design.

var n = 4
double(n)
print(n)

A Balanced Approach

Mojo blends both worlds: safe copies by default, deliberate mutation when you ask. You get safety and flexibility together.

What Comes Next

This is just a first taste. Later you will meet owned and the transfer operator for full control over how data moves. 🚀

Quick Check

You want a function to change the caller's actual value, not a copy. What do you use?

Recap

Mutating your own var is safe and local. To change a caller's value, opt in with inout; nothing mutates by accident. 🎯

よくある質問

「ミューテーションの基本」レッスンは無料ですか?

はい。「ミューテーションの基本」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「ミューテーションの基本」で何を学びますか?

他のコードを壊さずにデータを変更します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「ミューテーションの基本」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 値セマンティクスとは
  2. コピーと共有状態
  3. Mojoの安全なデフォルト
  4. ミューテーションの基本
← Mojo Academyに戻る