| 1 | <h2>Full example</h2> |
| 2 | <pre><code>const std = @import("std"); |
| 3 | |
| 4 | pub fn main() !void { |
| 5 | // If this program is run without stdout attached, exit with an error. |
| 6 | const stdout_file = try std.io.getStdOut(); |
| 7 | // If this program encounters pipe failure when printing to stdout, exit |
| 8 | // with an error. |
| 9 | try stdout_file.write("Hello, world!\n"); |
| 10 | } |
| 11 | |
| 12 | const warn = @import("std").debug.warn; |
| 13 | |
| 14 | pub fn main() void { |
| 15 | warn("Hello, world!\n"); |
| 16 | } |
| 17 | |
| 18 | const assert = @import("std").debug.assert; |
| 19 | |
| 20 | test "comments" { |
| 21 | // Comments in Zig start with "//" and end at the next LF byte (end of line). |
| 22 | // The below line is a comment, and won't be executed. |
| 23 | |
| 24 | //assert(false); |
| 25 | |
| 26 | const x = true; // another comment |
| 27 | assert(x); |
| 28 | } |
| 29 | |
| 30 | /// A structure for storing a timestamp, with nanosecond precision (this is a |
| 31 | /// multiline doc comment). |
| 32 | const Timestamp = struct { |
| 33 | /// The number of seconds since the epoch (this is also a doc comment). |
| 34 | seconds: i64, // signed so we can represent pre-1970 (not a doc comment) |
| 35 | /// The number of nanoseconds past the second (doc comment again). |
| 36 | nanos: u32, |
| 37 | |
| 38 | /// Returns a `Timestamp` struct representing the Unix epoch; that is, the |
| 39 | /// moment of 1970 Jan 1 00:00:00 UTC (this is a doc comment too). |
| 40 | pub fn unixEpoch() Timestamp { |
| 41 | return Timestamp{ |
| 42 | .seconds = 0, |
| 43 | .nanos = 0, |
| 44 | }; |
| 45 | } |
| 46 | };</code></pre> |