Comments
comments.zig
const expect = @import("std").testing.expect;
test "comments" {
// Comments in Zig start with "//" and end at the next LF byte (end of line).
// The below line is a comment, and won't be executed.
//expect(false);
const x = true; // another comment
expect(x);
}
$ zig test comments.zig
1/1 test "comments"... OK
All 1 tests passed.
There are no multiline comments in Zig (e.g. like /* */
comments in C). This helps allow Zig to have the property that each line of code can be tokenized out of context.
Doc comments
A doc comment is one that begins with exactly three slashes (i.e. ///
but not ////
); multiple doc comments in a row are merged together to form a multiline doc comment. The doc comment documents whatever immediately follows it.
/// A structure for storing a timestamp, with nanosecond precision (this is a
/// multiline doc comment).
const Timestamp = struct {
/// The number of seconds since the epoch (this is also a doc comment).
seconds: i64, // signed so we can represent pre-1970 (not a doc comment)
/// The number of nanoseconds past the second (doc comment again).
nanos: u32,
/// Returns a `Timestamp` struct representing the Unix epoch; that is, the
/// moment of 1970 Jan 1 00:00:00 UTC (this is a doc comment too).
pub fn unixEpoch() Timestamp {
return Timestamp{
.seconds = 0,
.nanos = 0,
};
}
};
Doc comments are only allowed in certain places; eventually, it will become a compile error to have a doc comment in an unexpected place, such as in the middle of an expression, or just before a non-doc comment.
Top-Level Doc Comments
User documentation that doesn’t belong to whatever immediately follows it, like package-level documentation, goes in top-level doc comments. A top-level doc comment is one that begins with two slashes and an exclamation point: //!
.
//! This module provides functions for retrieving the current date and
//! time with varying degrees of precision and accuracy. It does not
//! depend on libc, but will use functions from it if available.