TExcelTAD
Home › Zig › Zig Comments

Zig Comments

Comments are notes for humans; the compiler ignores them. Zig keeps comments simple: there is only one kind of ordinary comment, the line comment, plus two kinds of documentation comments that tools understand.

Line comments

A comment starts with // and runs to the end of the line. It can stand on its own line or follow code.

zig · try it yourself
const std = @import("std");

pub fn main() void {
    // Prices are stored in cents to avoid rounding problems.
    const price_cents = 1999;
    const quantity = 3; // how many the customer bought

    const total = price_cents * quantity;
    std.debug.print("total: {d} cents\n", .{total});
}
total: 5997 cents

No block comments

Unlike C, Zig has no /* … */ block comments. The reason is practical: a line comment can always be recognised by looking at a single line, which makes tools such as syntax highlighters and code reviewers simpler and avoids nested-comment confusion. To comment out several lines, put // at the start of each — every editor has a shortcut for this.

zig · try it yourself
const std = @import("std");

pub fn main() void {
    std.debug.print("step 1\n", .{});
    // std.debug.print("step 2\n", .{});
    // std.debug.print("step 3\n", .{});
    std.debug.print("step 4\n", .{});
}
step 1
step 4

Doc comments

A comment that starts with three slashes, ///, documents the declaration directly below it. zig build-exe -femit-docs and the standard library's online documentation are generated from these comments, and editors show them when you hover over a name.

zig · try it yourself
const std = @import("std");

/// Returns the area of a rectangle.
/// Both sides are given in metres.
fn area(width: u32, height: u32) u32 {
    return width * height;
}

pub fn main() void {
    std.debug.print("area = {d} m2\n", .{area(4, 5)});
}
area = 20 m2

A doc comment must be attached to something. A /// comment floating in the middle of a function, or at the end of a file, is a compile error:

zig · try it yourself
const std = @import("std");

pub fn main() void {
    /// this is not attached to a declaration
    std.debug.print("hi\n", .{});
}
./example.zig:4:5: error: expected statement, found 'a document comment'
    /// this is not attached to a declaration
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Top-level doc comments

//! documents the file itself (the module). It goes at the very top of the file, before any declarations:

zig
//! Utilities for parsing simple configuration files.
//! Each line has the form `key = value`.

const std = @import("std");

Summary

Syntax Purpose Where
// ordinary comment anywhere
/// doc comment for the next declaration directly above a function, constant, field, …
//! doc comment for the whole file top of the file

Tip: Good comments explain why, not what. total += tax; // add tax repeats the code; // tax is added after the discount, as required by law adds knowledge.

Test yourself

How do you comment out several lines in Zig?

  1. Wrap them in /* */
  2. Put // at the start of each line
  3. Use #if 0
  4. Wrap them in triple quotes

What is /// used for?

  1. A comment that is printed at runtime
  2. A doc comment for the following declaration
  3. A comment for the whole file
  4. Disabling a test