Skip to content

Commit 0db33e9

Browse files
mdsteeleandrewrk
authored andcommitted
Add "Comments" section to language reference (#1309)
The contents of this section come from the discussion on issue #1305.
1 parent 5d4a02c commit 0db33e9

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

doc/langref.html.in

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,58 @@ pub fn main() void {
134134
</p>
135135
{#see_also|Values|@import|Errors|Root Source File#}
136136
{#header_close#}
137+
{#header_open|Comments#}
138+
{#code_begin|test|comments#}
139+
const assert = @import("std").debug.assert;
140+
141+
test "comments" {
142+
// Comments in Zig start with "//" and end at the next LF byte (end of line).
143+
// The below line is a comment, and won't be executed.
144+
145+
//assert(false);
146+
147+
const x = true; // another comment
148+
assert(x);
149+
}
150+
{#code_end#}
151+
<p>
152+
There are no multiline comments in Zig (e.g. like <code>/* */</code>
153+
comments in C). This helps allow Zig to have the property that each line
154+
of code can be tokenized out of context.
155+
</p>
156+
{#header_open|Doc comments#}
157+
<p>
158+
A doc comment is one that begins with exactly three slashes (i.e.
159+
<code class="zig">///</code> but not <code class="zig">////</code>);
160+
multiple doc comments in a row are merged together to form a multiline
161+
doc comment. The doc comment documents whatever immediately follows it.
162+
</p>
163+
{#code_begin|syntax|doc_comments#}
164+
/// A structure for storing a timestamp, with nanosecond precision (this is a
165+
/// multiline doc comment).
166+
const Timestamp = struct {
167+
/// The number of seconds since the epoch (this is also a doc comment).
168+
seconds: i64, // signed so we can represent pre-1970 (not a doc comment)
169+
/// The number of nanoseconds past the second (doc comment again).
170+
nanos: u32,
171+
172+
/// Returns a `Timestamp` struct representing the Unix epoch; that is, the
173+
/// moment of 1970 Jan 1 00:00:00 UTC (this is a doc comment too).
174+
pub fn unixEpoch() Timestamp {
175+
return Timestamp{
176+
.seconds = 0,
177+
.nanos = 0,
178+
};
179+
}
180+
};
181+
{#code_end#}
182+
<p>
183+
Doc comments are only allowed in certain places; eventually, it will
184+
become a compile error have a doc comment in an unexpected place, such as
185+
in the middle of an expression, or just before a non-doc comment.
186+
</p>
187+
{#header_close#}
188+
{#header_close#}
137189
{#header_open|Values#}
138190
{#code_begin|exe|values#}
139191
const std = @import("std");

0 commit comments

Comments
 (0)