on 51c7eb7 help sign in
107 lines 2.5 kB history raw
1//! this is a top level doc, starts with "//!"
3const std = @import("std");
5pub fn main() anyerror!void {
6 const stdout = std.io.getStdOut().writer();
7 try stdout.print("Hello, {}!\n", .{"world"});
8}
10const expect = std.testing.expect;
12test "comments" {
13 // comments start with "//" until newline
14 // foo bar baz
15 const x = true; // another comment
16 expect(x);
19/// a doc comment starts with "///"
20/// multiple lines are merged together
21const Timestamp = struct {
22 /// number of seconds since epoch
23 seconds: i64,
25 /// number of nanoseconds past the second
26 nano: u32,
28 const Self = @This();
30 pub fn unixEpoch() Self {
31 return Self{
32 .seconds = 0,
33 .nanos = 0,
34 };
35 }
36};
38const my_val = switch (std.Target.current.os.tag) {
39 .linux => "Linux",
40 else => "not Linux",
41};
43const Book = enum {
44 paperback,
45 hardcover,
46 ebook,
47 pdf,
48};
50const TokenType = union(enum) {
51 int: isize,
52 float: f64,
53 string: []const u8,
54};
56const array_lit: [4]u8 = .{ 11, 22, 33, 44 };
57const sentinal_lit = [_:0]u8{ 1, 2, 3, 4 };
59test "address of syntax" {
60 // Get the address of a variable:
61 const x: i32 = 1234;
62 const x_ptr = &x;
64 // Dereference a pointer:
65 expect(x_ptr.* == 1234);
67 // When you get the address of a const variable, you get a const pointer to a single item.
68 expect(@TypeOf(x_ptr) == *const i32);
70 // If you want to mutate the value, you'd need an address of a mutable variable:
71 var y: i32 = 5678;
72 const y_ptr = &y;
73 expect(@TypeOf(y_ptr) == *i32);
74 y_ptr.* += 1;
75 expect(y_ptr.* == 5679);
78// integer literals
79const decimal_int = 98222;
80const hex_int = 0xff;
81const another_hex_int = 0xFF;
82const octal_int = 0o755;
83const binary_int = 0b11110000;
85// underscores may be placed between two digits as a visual separator
86const one_billion = 1_000_000_000;
87const binary_mask = 0b1_1111_1111;
88const permissions = 0o7_5_5;
89const big_address = 0xFF80_0000_0000_0000;
91// float literals
92const floating_point = 123.0E+77;
93const another_float = 123.0;
94const yet_another = 123.0e+77;
96const hex_floating_point = 0x103.70p-5;
97const another_hex_float = 0x103.70;
98const yet_another_hex_float = 0x103.70P-5;
100// underscores may be placed between two digits as a visual separator
101const lightspeed = 299_792_458.000_000;
102const nanosecond = 0.000_000_001;
103const more_hex = 0x1234_5678.9ABC_CDEFp-10;
105fn max(comptime T: type, a: T, b: T) T {
106 return if (a > b) a else b;