値の固定された集合にenumを使う
選択肢の閉じたリストに名前を付けます。
「値の固定された集合にenumを使う」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはZig Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Zig Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
A Closed List of Choices
Some values come from a small, fixed menu: a traffic light is red, yellow, or green. An enum names exactly that closed list of options.
Declaring an Enum
You write enum and list the names you want inside braces. Each name is one valid value of that type.
const Color = enum { red, green, blue };Enum Values Are Names
Each entry like red or blue is a real value of the type, not a string. You refer to it through the enum, keeping it tidy and typed.
const c: Color = Color.red;The Dot Shorthand
When Zig already knows the expected type, you can drop the name and just write a dot before the value.
const c: Color = .green;Comparing Enum Values
You compare enum values with == just like numbers. This makes branching on a fixed choice clean and readable.
if (c == .blue) {}Only Listed Values Are Allowed
An enum can hold only the names you declared. Try to use one that does not exist and the compiler rejects it right away.
const c: Color = .purple; // errorSelf-Documenting Code
Reading .green is far clearer than reading the number 1. Enums turn magic numbers into meaningful names you can trust.
Enums in Function Parameters
Accept an enum as a parameter and a caller can only pass one of your valid options, so bad input is caught at compile time.
fn paint(c: Color) void {}Switch Pairs Well with Enums
Enums shine with switch: you handle each named case, and Zig reminds you if you forget one of the options.
switch (c) { .red => {}, .green => {}, .blue => {} }Naming Conventions
Enum types use TitleCase like Color, while the individual values use lower snake_case like light_blue. This keeps the two roles distinct.
Why Reach for an Enum
Whenever a value is one of a few known states, an enum makes the choices explicit, safe, and impossible to misspell silently.
Quick Check
You want a type that is exactly one of red, green, or blue. What fits?
Recap
You met enums: a type that names a fixed set of values. Use the dot shorthand, compare with ==, and let the compiler block invalid choices. 🚦
よくある質問
「値の固定された集合にenumを使う」レッスンは無料ですか?
はい。「値の固定された集合にenumを使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。
「値の固定された集合にenumを使う」で何を学びますか?
選択肢の閉じたリストに名前を付けます。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Zig Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「値の固定された集合にenumを使う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このZig Academyレッスンでコードを書いて実行できますか?
はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 値の固定された集合にenumを使う
- 複数の型のいずれかを保持するユニオン
- タグ付きユニオンとswitch
- enumのメソッドと基底整数型