決して変わらない値にはconstを使う
デフォルトで不変のバインディングを使います。
「決して変わらない値にはconstを使う」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはZig Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Zig Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Bindings Start Here
In Zig, you give a value a name with a binding. The first choice you make is whether that name is allowed to change later.
Meet const
The keyword const creates a name whose value can never be reassigned. It is the safe, default way to name things in Zig.
const pi = 3.14159;Immutable by Choice
A const binding is immutable: once it points at a value, that link is locked. This makes your code easier to read and reason about.
Reassigning Is an Error
Try to give a const a new value and the compiler stops you. Zig reports a clear error instead of silently letting the bug through.
const x = 5;
x = 6; // error: cannot assign to constantInitialize at Declaration
You set a const's value right where you declare it. There is no empty const waiting to be filled in later on.
const greeting = "Hello, Zig";const for Configuration
Fixed numbers like a buffer size or a max count are perfect for const. They name a value clearly and guarantee it stays put.
const max_users = 100;Prefer const First
Good Zig style reaches for const first and only loosens up when a value truly must change. Less mutation means fewer surprises.
Type Is Still Inferred
You usually do not write a type after const. Zig infers it from the value, so const count = 0 just works as an integer.
const count = 0;const at the Top Level
Constants declared outside any function become file-wide globals. They are evaluated once and shared across your whole program.
const version = "1.0.0";Importing Returns a const
You will often see const std = @import. The result of an import is a value, and binding it with const keeps that module fixed.
const std = @import("std");Self-Documenting Code
Seeing const tells any reader the value will not move. That single keyword acts as a small, trustworthy promise about your intent.
Quick Check
You declare const limit = 10. What happens if you later write limit = 20?
Recap
You learned that const names a value that never changes. Initialize it once, let Zig infer the type, and reach for it as your default. 🔒
よくある質問
「決して変わらない値にはconstを使う」レッスンは無料ですか?
はい。「決して変わらない値にはconstを使う」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。
「決して変わらない値にはconstを使う」で何を学びますか?
デフォルトで不変のバインディングを使います。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Zig Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「決して変わらない値にはconstを使う」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このZig Academyレッスンでコードを書いて実行できますか?
はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 決して変わらない値にはconstを使う
- 可変状態にはvarを使う
- 型推論と明示的な型指定
- undefinedと未初期化メモリ