Skip to content

Exercises

1. Write a program that computes several Fibonacci numbers with comptime. Print the results at runtime.

  1. Write a program that computes several Fibonacci numbers with comptime. Print the results at runtime.

  2. Rewrite this declaration so the value is computed during compilation:

const x = 10 * 20;
  1. Explain why this code is invalid:
var n: usize = 4;
const a = comptime n * 2;
  1. Write a function:
fn square(x: i32) i32

Call it both at runtime and with comptime.

  1. Write a generic function:
fn zero(comptime T: type) T

Return the zero value of the given type.

  1. Write a generic function:
fn max(comptime T: type, a: T, b: T) T

Test it with integers and floating-point values.

  1. Write a function:
fn Array(comptime T: type, comptime N: usize) type

Return the type [N]T.

  1. Write a function:
fn Pair(comptime T: type) type

Return a struct with fields first and second.

  1. Add methods swap and print to the type returned by Pair.

  2. Write a function:

fn Buffer(comptime N: usize) type

The returned type should contain:

data: [N]u8
len: usize
  1. Add methods append and clear to Buffer.

  2. Write a function:

fn isInteger(comptime T: type) bool

Use @typeInfo.

  1. Write a function:
fn isFloat(comptime T: type) bool

Use @typeInfo.

  1. Write a function that prints the names and sizes of these types:
u8
u16
u32
u64

Use inline for.

  1. Write an inline loop over this tuple:
.{ 10, true, "zig" }

Print each value with {any}.

  1. Write a function:
fn fieldCount(comptime T: type) usize

Return the number of fields in a struct.

  1. Write a function:
fn printFields(comptime T: type) void

Print all field names and field types in a struct.

  1. Write a function:
fn enumTagCount(comptime T: type) usize

Return the number of tags in an enum.

  1. Write a function:
fn printEnumTags(comptime T: type) void

Print all enum tag names.

  1. Write a compile-time check that rejects systems where:
@sizeOf(usize) < 8
  1. Write a generic stack type:
fn Stack(comptime T: type, comptime N: usize) type

Implement:

  • push
  • pop
  • peek
  • clear
  1. Modify Stack so it reports overflow with an error.

  2. Write a type generator:

fn Matrix(comptime T: type, comptime rows: usize, comptime cols: usize) type

Store values in:

[rows][cols]T
  1. Write a generic function:
fn swap(comptime T: type, a: *T, b: *T) void
  1. Write a function:
fn hasField(comptime T: type, comptime name: []const u8) bool

Return whether a struct contains the named field.

  1. Write a compile-time assertion that requires a struct to contain a field named id.

  2. Write a function:

fn describe(comptime T: type) void

Print whether the type is an integer, float, pointer, struct, enum, or union.

  1. Write a type generator that returns one struct when a compile-time boolean is true and another when it is false.

  2. Explain the difference between:

for

and:

inline for
  1. Explain why Zig uses ordinary functions for generic programming instead of a separate template language.