0Pricing
Zig Academy · 课时

类型推断与显式类型

让 Zig 推断类型,或由您自行标注。

类型推断与显式类型 是 CoddyKit 上的免费 Zig Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Zig Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Zig Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Two Ways to Type

When you bind a value, Zig can figure out the type for you, or you can spell it out. Both rely on Zig's strong, static type system.

Inference for Clear Values

If the value makes the type obvious, let Zig infer it. const name = "Ada" is clearly text, so no annotation is needed.

const name = "Ada";

Annotating Explicitly

To state a type yourself, write a colon and the type after the name. This is how you pin down an exact integer width.

const age: u8 = 30;

Default Integer Type

An inferred integer literal becomes a comptime_int until you use it. When it lands in a slot, Zig coerces it to the needed width.

const small = 7; // comptime_int

When You Must Annotate

Sometimes the value alone is not enough. A var starting at 0 needs an explicit type so Zig knows whether it is a u8 or an i64.

var counter: u32 = 0;

Floats Need Care Too

A bare 3.14 is a comptime_float. Annotate it to choose f32 or f64 when precision and memory size actually matter to you.

const ratio: f32 = 3.14;

Inference Aids Readability

When the type is obvious from context, dropping it keeps lines clean. Annotate only when it adds real clarity or is required.

Coercion Follows the Target

Assign 5 to a u16 slot and the literal quietly fits. Zig performs safe coercion toward the declared type whenever it can.

const n: u16 = 5;

Explicit at Boundaries

At function signatures and data structures, types are always explicit. Inference is a local convenience, not a way to skip declarations.

fn area(w: u32, h: u32) u32 {}

No Surprising Conversions

Zig will not silently turn a float into an int for you. Mismatched types raise an error, keeping numeric behavior predictable.

A Simple Habit

Infer when the value speaks for itself; annotate when the type is the information a reader needs. Clarity should drive the choice.

Quick Check

You write var counter = 0; and Zig complains. Why does it want more from you?

Recap

You saw that Zig can infer a type from a clear value, or you can annotate one with a colon. Let clarity and required cases decide which to use. 🎯

常见问题解答

「类型推断与显式类型」课时是免费的吗?

是的 — 「类型推断与显式类型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Zig Academy 课程的其余内容,请升级到 CoddyKit PRO。 Zig Academy 课程共包含 4 节课。

「类型推断与显式类型」这节课中我会学到什么?

让 Zig 推断类型,或由您自行标注。 你通过在浏览器中直接运行的动手代码来练习 Zig Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Zig Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Zig Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「类型推断与显式类型」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Zig Academy 课中编写并运行代码吗?

能。每节 Zig Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 const 保存永不改变的值
  2. 使用 var 保存可变状态
  3. 类型推断与显式类型
  4. undefined 与未初始化内存
← 返回 Zig Academy