Zig WASM Factorial - Zig-wasm Typing CST Test
Loading…
Zig WASM Factorial — Zig-wasm Code
Calculates factorial recursively and prints the result.
# 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)});
}Zig-wasm Language Guide
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.
Primary Use Cases
- ▸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
Notable Features
- ▸Direct compilation to WebAssembly (Wasm32 target)
- ▸No hidden runtime or garbage collector
- ▸C interop for leveraging existing code
- ▸Manual memory management with safety options
- ▸Simple, readable, and maintainable syntax
Origin & Creator
Zig is created by Andrew Kelley, with the community contributing to language development, compiler improvements, and WebAssembly support.
Industrial Note
Zig-Wasm is particularly useful for developers who need predictable, high-performance WebAssembly modules, low-level memory control, or integration with C/C++ codebases for web execution.