Comments

comments.zig

  1. const assert = @import("std").debug.assert;
  2. test "comments" {
  3. // Comments in Zig start with "//" and end at the next LF byte (end of line).
  4. // The below line is a comment, and won't be executed.
  5. //assert(false);
  6. const x = true; // another comment
  7. assert(x);
  8. }
  1. $ zig test comments.zig
  2. 1/1 test "comments"...OK
  3. All 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.

  1. /// A structure for storing a timestamp, with nanosecond precision (this is a
  2. /// multiline doc comment).
  3. const Timestamp = struct {
  4. /// The number of seconds since the epoch (this is also a doc comment).
  5. seconds: i64, // signed so we can represent pre-1970 (not a doc comment)
  6. /// The number of nanoseconds past the second (doc comment again).
  7. nanos: u32,
  8. /// Returns a `Timestamp` struct representing the Unix epoch; that is, the
  9. /// moment of 1970 Jan 1 00:00:00 UTC (this is a doc comment too).
  10. pub fn unixEpoch() Timestamp {
  11. return Timestamp{
  12. .seconds = 0,
  13. .nanos = 0,
  14. };
  15. }
  16. };

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.