0Pricing
Zig Academy · レッスン

可変状態にはvarを使う

再代入を許可するタイミングと方法を学びます。

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

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

When Values Change

Some values truly need to move: a counter, a running total, a current score. For those, Zig gives you the var keyword.

Meet var

The keyword var creates a mutable binding. Unlike const, the name you declare with var can be reassigned again and again.

var score = 0;

Reassigning Freely

Once a value is a var, updating it is just a plain assignment. No special syntax is needed, only the name and a new value.

var score = 0;
score = 10;

Updating in Place

You can compute a new value from the old one. Adding to a counter is the classic example of useful mutation in a loop.

var count: u32 = 0;
count += 1;

Type Often Needed

For a var that starts at zero, Zig may ask for a type so it knows the integer width. Annotate it once and you are set.

var total: i64 = 0;

Unused Mutation Warns

If you declare a var but never actually change it, Zig warns you. It nudges you to use const instead for a clearer intent.

var Inside Functions

Mutable locals live inside functions and last for that scope. A loop accumulator declared with var is a perfect everyday use.

var sum: u32 = 0;
sum += value;

The Cost of Mutation

Every var is one more thing that can change underfoot. Use it where change is the point, not as a lazy default choice.

Loop Counters

A while loop often drives a var forward each pass. The binding updates, the condition rechecks, and the loop advances.

var i: usize = 0;
while (i < 5) : (i += 1) {}

Same Type, New Value

A var can change its value but never its type. An i32 stays an i32; you cannot suddenly store text in a number binding.

var vs const Recap

Think of it simply: const is a value pinned in place, while var is a value that is meant to move. Reach for var only when it must.

Quick Check

You need a counter that increases inside a loop. Which keyword should declare it?

Recap

You learned that var creates a mutable binding for values that change. Annotate its type when needed, and use it only where mutation is truly the goal. 🔁

よくある質問

「可変状態にはvarを使う」レッスンは無料ですか?

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

「可変状態にはvarを使う」で何を学びますか?

再代入を許可するタイミングと方法を学びます。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「可変状態にはvarを使う」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. 決して変わらない値にはconstを使う
  2. 可変状態にはvarを使う
  3. 型推論と明示的な型指定
  4. undefinedと未初期化メモリ
← Zig Academyに戻る