Learn Zig-wasm - 10 Code Examples & CST Typing Practice Test
Zig is a general-purpose programming language designed for robustness, optimality, and simplicity. With Zig-Wasm, developers can compile Zig code to WebAssembly, enabling high-performance, low-level applications in the browser or other Wasm runtimes.
Learn ZIG-WASM with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Simple Zig WebAssembly Program
# zig/demo/main.zig
const std = @import("std");
pub export fn main() void {
std.debug.print("Hello, Zig WASM!\n", .{});
}
A basic Zig program compiled to WebAssembly that prints 'Hello, Zig WASM!' to the console.
Zig WASM Add Two Numbers
# zig/demo/add.zig
const std = @import("std");
pub export fn add(a: i32, b: i32) void {
std.debug.print("Sum: {d}\n", .{a + b});
}
Adds two integers and prints the result.
Zig WASM Multiply Two Numbers
# zig/demo/multiply.zig
const std = @import("std");
pub export fn multiply(a: i32, b: i32) void {
std.debug.print("Product: {d}\n", .{a * b});
}
Multiplies two integers and prints the result.
Zig WASM Fibonacci
# zig/demo/fibonacci.zig
const std = @import("std");
fn fib(n: i32) i32 {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
pub export fn fibonacci(n: i32) void {
std.debug.print("Fibonacci: {d}\n", .{fib(n)});
}
Calculates the nth Fibonacci number recursively and prints it.
Zig WASM Factorial
# zig/demo/factorial.zig
const std = @import("std");
fn factorial(n: i32) i32 {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
pub export fn factorial(n: i32) void {
std.debug.print("Factorial: {d}\n", .{factorial(n)});
}
Calculates factorial recursively and prints the result.
Zig WASM Even Check
# zig/demo/even.zig
const std = @import("std");
pub export fn isEven(n: i32) void {
if (n % 2 == 0) {
std.debug.print("Even\n", .{});
} else {
std.debug.print("Odd\n", .{});
}
}
Checks if a number is even and prints the result.
Zig WASM Maximum of Two Numbers
# zig/demo/max.zig
const std = @import("std");
pub export fn max(a: i32, b: i32) void {
if (a > b) {
std.debug.print("Max: {d}\n", .{a});
} else {
std.debug.print("Max: {d}\n", .{b});
}
}
Finds the maximum of two numbers and prints it.
Zig WASM Minimum of Two Numbers
# zig/demo/min.zig
const std = @import("std");
pub export fn min(a: i32, b: i32) void {
if (a < b) {
std.debug.print("Min: {d}\n", .{a});
} else {
std.debug.print("Min: {d}\n", .{b});
}
}
Finds the minimum of two numbers and prints it.
Zig WASM Toggle Boolean
# zig/demo/toggle.zig
const std = @import("std");
var state: bool = false;
pub export fn toggle() void {
state = !state;
if (state) {
std.debug.print("ON\n", .{});
} else {
std.debug.print("OFF\n", .{});
}
}
Toggles a boolean value and prints ON/OFF.
Zig WASM Print Array
# zig/demo/array.zig
const std = @import("std");
pub export fn printArray(arr: [5]i32) void {
for (arr) |val| {
std.debug.print("{d} ", .{val});
}
std.debug.print("\n", .{});
}
Prints elements of an array.
Frequently Asked Questions about Zig-wasm
What is Zig-wasm?
Zig is a general-purpose programming language designed for robustness, optimality, and simplicity. With Zig-Wasm, developers can compile Zig code to WebAssembly, enabling high-performance, low-level applications in the browser or other Wasm runtimes.
What are the primary use cases for Zig-wasm?
Porting system-level libraries to WebAssembly. High-performance game engines or simulations in the browser. Cryptography, compression, or other CPU-intensive algorithms. Replacing C/C++ Wasm modules with safer, simpler Zig code. Low-level WASI (WebAssembly System Interface) applications
What are the strengths of Zig-wasm?
Predictable and deterministic performance. Safe alternative to C for low-level WebAssembly. Small runtime footprint, ideal for Wasm. Easier debugging than C/C++ in Wasm. High interoperability with other languages and platforms
What are the limitations of Zig-wasm?
Smaller ecosystem compared to Rust or JS frameworks. No built-in reactive or UI framework. Requires manual memory and resource management. Limited high-level abstractions for web development. Debugging in browser WebAssembly can be challenging
How can I practice Zig-wasm typing speed?
CodeSpeedTest offers 10+ real Zig-wasm code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.