0Pricing
Zig Academy · Lesson

Type Inference vs Explicit Types

Let Zig infer or annotate yourself.

Type Inference vs Explicit Types is a free Zig Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🎯

Frequently asked questions

Is the “Type Inference vs Explicit Types” lesson free?

Yes — the full text of “Type Inference vs Explicit Types” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Type Inference vs Explicit Types”?

Let Zig infer or annotate yourself. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Type Inference vs Explicit Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. const for Values That Never Change
  2. var for Mutable State
  3. Type Inference vs Explicit Types
  4. undefined and Uninitialized Memory
← Back to Zig Academy