msfjarvis / msfjarvis.dev

Hugo source for my blog
https://msfjarvis.dev
8 stars 3 forks source link

posts/learning-zig-day-2/ #45

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Learning Zig - Day 2 - Harsh Shandilya

Onwards in our quest to learn Zig

https://msfjarvis.dev/posts/learning-zig-day-2/

komuw commented 3 years ago

The only problem I encountered here was that I can’t figure out how to print an array!

If you try;

const std = @import("std");

pub fn main() void {
  const implicitly_sized_array = [_]u8{0, 1, 2, 3};
  std.debug.print("This is an array: {}\n", .{implicitly_sized_array});
}

on master branch, you get a helpful compiler error:

error: cannot format array without a specifier (i.e. {s} or {any})
                @compileError("cannot format array without a specifier (i.e. {s} or {any})");
               ^

so when we change our code to;

std.debug.print("This is an array: {any}\n", .{implicitly_sized_array});

we get;

This is an array: { 0, 1, 2, 3 }

see: https://zig.godbolt.org/z/68fecP567

msfjarvis commented 3 years ago

The only problem I encountered here was that I can’t figure out how to print an array!

If you try;

const std = @import("std");

pub fn main() void {
  const implicitly_sized_array = [_]u8{0, 1, 2, 3};
  std.debug.print("This is an array: {}\n", .{implicitly_sized_array});
}

on master branch, you get a helpful compiler error:

error: cannot format array without a specifier (i.e. {s} or {any})
                @compileError("cannot format array without a specifier (i.e. {s} or {any})");
               ^

so when we change our code to;

std.debug.print("This is an array: {any}\n", .{implicitly_sized_array});

we get;

This is an array: { 0, 1, 2, 3 }

see: zig.godbolt.org/z/68fecP567

I suppose that's what I get for trying to use the stable branch. Will make the switch to nightly, thanks for the tip!