aboutsummaryrefslogtreecommitdiff
path: root/examples/hello-zig/src/main.zig
blob: 74eb5901df76b3429ad1253451deeb3817659f2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const std = @import("std");
const hello_zig = @import("hello_zig");

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    defer arena.deinit();

    const allocator = arena.allocator();
    const args = try std.process.argsAlloc(allocator);
    const name = if (args.len > 1) args[1] else "Nova";

    var greeter = hello_zig.Greeter{
        .name = name,
        .punctuation = '!',
    };

    const stdout = std.fs.File.stdout().deprecatedWriter();
    try greeter.write(stdout, .verbose);

    const sample_values = [_]i32{ -3, -2, 0, 1, 2, 3, 4 };
    var squares = try hello_zig.collectEvenSquares(allocator, &sample_values);
    defer squares.deinit(allocator);

    std.debug.print("even squares: {any}\n", .{squares.items});
}

test "main module can compute a summary" {
    const greeting = try hello_zig.describeNumber(std.testing.allocator, 42);
    defer std.testing.allocator.free(greeting);

    try std.testing.expect(std.mem.startsWith(u8, greeting, "positive"));
}