bool、void 与 comptime 类型
掌握这些小巧但必不可少的基本类型。
bool、void 与 comptime 类型 是 CoddyKit 上的免费 Zig Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Zig Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Zig Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Small but Essential Types
Beyond numbers, Zig has a handful of tiny primitives that show up everywhere: bool, void, and special compile-time number types. 🧩
bool Holds True or False
A bool stores exactly one of two values: true or false. It drives every if statement and while condition you write.
const ready: bool = true;Booleans Come from Comparisons
Comparison operators produce booleans. The expression 5 > 3 evaluates to true, which you can store or test directly.
const bigger = 5 > 3; // truevoid Means No Value
The void type represents the absence of a value. A function returning void hands back nothing meaningful when it finishes.
fn log() void {
// does work, returns nothing
}Where void Shows Up
You see void as the return type of side-effect functions, and as the payload of containers that only care that a key exists.
comptime_int for Whole Literals
An untyped whole number like 42 starts as a comptime_int. It has arbitrary precision and only gains a fixed width when assigned.
const n = 42; // comptime_int until usedcomptime_float for Decimals
Similarly, an untyped decimal such as 3.14 is a comptime_float. It is exact at compile time before it becomes an f32 or f64.
const ratio = 3.14; // comptime_floatComptime Numbers Are Flexible
Because they are compile-time only, these types can hold huge or tiny values precisely. They coerce into sized types when you use them.
The type Type
In Zig a type is itself a value with the type named type. This is what lets functions accept and return types for generics.
const T: type = i32;anyopaque for Unknown Data
The anyopaque type marks memory whose shape is unknown, often used at C boundaries where the layout is hidden from Zig.
Why These Primitives Matter
Together these small types give Zig its foundation: booleans for logic, void for nothing, and comptime numbers for flexible literals.
Quick Check
You write the literal 100 with no type annotation. Before it is assigned anywhere, what type does Zig give it?
Recap
Remember the small primitives: bool for true or false, void for no value, and comptime_int and comptime_float for flexible literals. 🎯
常见问题解答
「bool、void 与 comptime 类型」课时是免费的吗?
是的 — 「bool、void 与 comptime 类型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Zig Academy 课程的其余内容,请升级到 CoddyKit PRO。 Zig Academy 课程共包含 4 节课。
「bool、void 与 comptime 类型」这节课中我会学到什么?
掌握这些小巧但必不可少的基本类型。 你通过在浏览器中直接运行的动手代码来练习 Zig Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Zig Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Zig Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「bool、void 与 comptime 类型」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Zig Academy 课中编写并运行代码吗?
能。每节 Zig Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 定长整数:i32、u8、usize
- 浮点数:f32 与 f64
- bool、void 与 comptime 类型
- 数字字面量与下划线