Skip to main content Svelte Highlight v7.21.1

Zig language preview

1. Main with @import

@import, try, and !void
horizon-dark
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}
atom-one-dark
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}
github-dark
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}
dracula
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}
nord
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}
github
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}

2. Comptime struct

struct methods and comptime List(T)
horizon-dark
const Point = struct {
    x: f32,
    y: f32,

    pub fn distance(self: Point, other: Point) f32 {
        const dx = self.x - other.x;
        const dy = self.y - other.y;
        return @sqrt(dx * dx + dy * dy);
    }
};

fn List(comptime T: type) type {
    return struct {
        items: []T,
        len: usize = 0,
    };
}
atom-one-dark
const Point = struct {
    x: f32,
    y: f32,

    pub fn distance(self: Point, other: Point) f32 {
        const dx = self.x - other.x;
        const dy = self.y - other.y;
        return @sqrt(dx * dx + dy * dy);
    }
};

fn List(comptime T: type) type {
    return struct {
        items: []T,
        len: usize = 0,
    };
}
github-dark
const Point = struct {
    x: f32,
    y: f32,

    pub fn distance(self: Point, other: Point) f32 {
        const dx = self.x - other.x;
        const dy = self.y - other.y;
        return @sqrt(dx * dx + dy * dy);
    }
};

fn List(comptime T: type) type {
    return struct {
        items: []T,
        len: usize = 0,
    };
}
dracula
const Point = struct {
    x: f32,
    y: f32,

    pub fn distance(self: Point, other: Point) f32 {
        const dx = self.x - other.x;
        const dy = self.y - other.y;
        return @sqrt(dx * dx + dy * dy);
    }
};

fn List(comptime T: type) type {
    return struct {
        items: []T,
        len: usize = 0,
    };
}
nord
const Point = struct {
    x: f32,
    y: f32,

    pub fn distance(self: Point, other: Point) f32 {
        const dx = self.x - other.x;
        const dy = self.y - other.y;
        return @sqrt(dx * dx + dy * dy);
    }
};

fn List(comptime T: type) type {
    return struct {
        items: []T,
        len: usize = 0,
    };
}
github
const Point = struct {
    x: f32,
    y: f32,

    pub fn distance(self: Point, other: Point) f32 {
        const dx = self.x - other.x;
        const dy = self.y - other.y;
        return @sqrt(dx * dx + dy * dy);
    }
};

fn List(comptime T: type) type {
    return struct {
        items: []T,
        len: usize = 0,
    };
}

3. Literals and switch

0x/0b literals, enum, switch, \\ strings
horizon-dark
const Color = enum { red, green, blue };

const mask: u32 = 0xFF_00;
const flags: u8 = 0b1010_0101;

fn describe(c: Color) []const u8 {
    return switch (c) {
        .red => "warm",
        .green, .blue => "cool",
    };
}

const banner =
    \\Welcome
    \\to Zig
;
atom-one-dark
const Color = enum { red, green, blue };

const mask: u32 = 0xFF_00;
const flags: u8 = 0b1010_0101;

fn describe(c: Color) []const u8 {
    return switch (c) {
        .red => "warm",
        .green, .blue => "cool",
    };
}

const banner =
    \\Welcome
    \\to Zig
;
github-dark
const Color = enum { red, green, blue };

const mask: u32 = 0xFF_00;
const flags: u8 = 0b1010_0101;

fn describe(c: Color) []const u8 {
    return switch (c) {
        .red => "warm",
        .green, .blue => "cool",
    };
}

const banner =
    \\Welcome
    \\to Zig
;
dracula
const Color = enum { red, green, blue };

const mask: u32 = 0xFF_00;
const flags: u8 = 0b1010_0101;

fn describe(c: Color) []const u8 {
    return switch (c) {
        .red => "warm",
        .green, .blue => "cool",
    };
}

const banner =
    \\Welcome
    \\to Zig
;
nord
const Color = enum { red, green, blue };

const mask: u32 = 0xFF_00;
const flags: u8 = 0b1010_0101;

fn describe(c: Color) []const u8 {
    return switch (c) {
        .red => "warm",
        .green, .blue => "cool",
    };
}

const banner =
    \\Welcome
    \\to Zig
;
github
const Color = enum { red, green, blue };

const mask: u32 = 0xFF_00;
const flags: u8 = 0b1010_0101;

fn describe(c: Color) []const u8 {
    return switch (c) {
        .red => "warm",
        .green, .blue => "cool",
    };
}

const banner =
    \\Welcome
    \\to Zig
;

4. C interop with anyopaque

anyopaque, the 0.10+ replacement for c_void
horizon-dark
const std = @import("std");

extern fn c_alloc(size: usize) *anyopaque;
extern fn c_free(ptr: *anyopaque) void;

fn withOpaqueHandle(handle: *anyopaque) void {
    const bytes: [*]u8 = @ptrCast(handle);
    std.debug.print("first byte: {d}\n", .{bytes[0]});
}
atom-one-dark
const std = @import("std");

extern fn c_alloc(size: usize) *anyopaque;
extern fn c_free(ptr: *anyopaque) void;

fn withOpaqueHandle(handle: *anyopaque) void {
    const bytes: [*]u8 = @ptrCast(handle);
    std.debug.print("first byte: {d}\n", .{bytes[0]});
}
github-dark
const std = @import("std");

extern fn c_alloc(size: usize) *anyopaque;
extern fn c_free(ptr: *anyopaque) void;

fn withOpaqueHandle(handle: *anyopaque) void {
    const bytes: [*]u8 = @ptrCast(handle);
    std.debug.print("first byte: {d}\n", .{bytes[0]});
}
dracula
const std = @import("std");

extern fn c_alloc(size: usize) *anyopaque;
extern fn c_free(ptr: *anyopaque) void;

fn withOpaqueHandle(handle: *anyopaque) void {
    const bytes: [*]u8 = @ptrCast(handle);
    std.debug.print("first byte: {d}\n", .{bytes[0]});
}
nord
const std = @import("std");

extern fn c_alloc(size: usize) *anyopaque;
extern fn c_free(ptr: *anyopaque) void;

fn withOpaqueHandle(handle: *anyopaque) void {
    const bytes: [*]u8 = @ptrCast(handle);
    std.debug.print("first byte: {d}\n", .{bytes[0]});
}
github
const std = @import("std");

extern fn c_alloc(size: usize) *anyopaque;
extern fn c_free(ptr: *anyopaque) void;

fn withOpaqueHandle(handle: *anyopaque) void {
    const bytes: [*]u8 = @ptrCast(handle);
    std.debug.print("first byte: {d}\n", .{bytes[0]});
}

5. Optional types

?T optional prefix alongside !T error unions
horizon-dark
fn find(items: []const i32, target: i32) ?usize {
    for (items, 0..) |item, i| {
        if (item == target) return i;
    }
    return null;
}

fn describe(items: []const i32, target: i32) ![]const u8 {
    const index = find(items, target) orelse return error.NotFound;
    return if (index == 0) "first" else "found";
}
atom-one-dark
fn find(items: []const i32, target: i32) ?usize {
    for (items, 0..) |item, i| {
        if (item == target) return i;
    }
    return null;
}

fn describe(items: []const i32, target: i32) ![]const u8 {
    const index = find(items, target) orelse return error.NotFound;
    return if (index == 0) "first" else "found";
}
github-dark
fn find(items: []const i32, target: i32) ?usize {
    for (items, 0..) |item, i| {
        if (item == target) return i;
    }
    return null;
}

fn describe(items: []const i32, target: i32) ![]const u8 {
    const index = find(items, target) orelse return error.NotFound;
    return if (index == 0) "first" else "found";
}
dracula
fn find(items: []const i32, target: i32) ?usize {
    for (items, 0..) |item, i| {
        if (item == target) return i;
    }
    return null;
}

fn describe(items: []const i32, target: i32) ![]const u8 {
    const index = find(items, target) orelse return error.NotFound;
    return if (index == 0) "first" else "found";
}
nord
fn find(items: []const i32, target: i32) ?usize {
    for (items, 0..) |item, i| {
        if (item == target) return i;
    }
    return null;
}

fn describe(items: []const i32, target: i32) ![]const u8 {
    const index = find(items, target) orelse return error.NotFound;
    return if (index == 0) "first" else "found";
}
github
fn find(items: []const i32, target: i32) ?usize {
    for (items, 0..) |item, i| {
        if (item == target) return i;
    }
    return null;
}

fn describe(items: []const i32, target: i32) ![]const u8 {
    const index = find(items, target) orelse return error.NotFound;
    return if (index == 0) "first" else "found";
}