zig
General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
Top Related Projects
Empowering everyone to build reliable and efficient software.
The Go programming language
Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
The Crystal Programming Language
Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
The Kotlin Programming Language.
Quick Overview
Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software. It aims to be a better C, focusing on simplicity, performance, and safety. Zig provides low-level control with high-level abstractions and compile-time features.
Pros
- Simplicity and readability: Zig has a clean syntax and avoids hidden control flow
- Performance: Zig compiles to efficient machine code and provides fine-grained control over memory
- Cross-compilation: Easily compile for different target architectures and operating systems
- Powerful compile-time features: Supports compile-time code execution and metaprogramming
Cons
- Relatively new language: Ecosystem and community are still growing
- Learning curve: Some concepts may be unfamiliar to developers coming from other languages
- Limited IDE support: Tooling and editor integration are still developing
- Ongoing language evolution: Some features and APIs may change as the language matures
Code Examples
- Hello World:
const std = @import("std");
pub fn main() !void {
std.debug.print("Hello, World!\n", .{});
}
- Error handling:
const std = @import("std");
fn divideNumbers(a: i32, b: i32) !i32 {
if (b == 0) return error.DivisionByZero;
return a / b;
}
pub fn main() !void {
const result = try divideNumbers(10, 2);
std.debug.print("Result: {}\n", .{result});
}
- Compile-time reflection:
const std = @import("std");
fn printTypeInfo(comptime T: type) void {
const info = @typeInfo(T);
std.debug.print("Type: {s}\n", .{@typeName(T)});
std.debug.print("Size: {} bytes\n", .{@sizeOf(T)});
std.debug.print("Alignment: {} bytes\n", .{@alignOf(T)});
}
pub fn main() void {
printTypeInfo(i32);
}
Getting Started
- Install Zig: Download from https://ziglang.org/download/
- Create a new file
hello.zig
:
const std = @import("std");
pub fn main() !void {
std.debug.print("Hello, Zig!\n", .{});
}
- Compile and run:
zig run hello.zig
For more advanced usage, refer to the official Zig documentation at https://ziglang.org/documentation/master/.
Competitor Comparisons
Empowering everyone to build reliable and efficient software.
Pros of Rust
- More mature ecosystem with a larger community and extensive library support
- Advanced memory safety features without garbage collection
- Powerful type system and trait-based generics
Cons of Rust
- Steeper learning curve due to complex concepts like lifetimes and borrowing
- Longer compile times, especially for large projects
- More verbose syntax compared to Zig's simplicity
Code Comparison
Rust:
fn main() {
let numbers = vec![1, 2, 3, 4, 5];
let sum: i32 = numbers.iter().sum();
println!("Sum: {}", sum);
}
Zig:
const std = @import("std");
pub fn main() !void {
const numbers = [_]i32{ 1, 2, 3, 4, 5 };
var sum: i32 = 0;
for (numbers) |num| {
sum += num;
}
std.debug.print("Sum: {}\n", .{sum});
}
Summary
Both Rust and Zig are systems programming languages aimed at providing safety and performance. Rust offers a more established ecosystem and advanced features, but comes with a steeper learning curve. Zig focuses on simplicity and ease of use, making it potentially more approachable for beginners. The choice between the two depends on project requirements, team expertise, and desired language features.
The Go programming language
Pros of Go
- Mature ecosystem with extensive libraries and tools
- Strong support for concurrency with goroutines and channels
- Faster compilation times for large projects
Cons of Go
- Less control over memory management
- More verbose syntax in some cases
- Limited generics support (though improving)
Code Comparison
Go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Zig:
const std = @import("std");
pub fn main() void {
std.debug.print("Hello, World!\n", .{});
}
Summary
Go offers a mature ecosystem and excellent concurrency support, making it well-suited for large-scale projects and web services. Zig, while newer, provides more fine-grained control over memory and a simpler syntax. Go's compilation speed is generally faster for large projects, but Zig aims to improve in this area. Both languages prioritize simplicity and readability, but Zig offers more low-level control. The choice between them depends on project requirements, team expertise, and specific performance needs.
Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
Pros of V
- Simpler syntax and faster compilation times
- Built-in package management and module system
- Automatic memory management without garbage collection
Cons of V
- Smaller community and ecosystem compared to Zig
- Less mature and stable, with more frequent breaking changes
- Limited support for low-level system programming
Code Comparison
V:
fn main() {
println('Hello, World!')
}
Zig:
pub fn main() void {
std.debug.print("Hello, World!\n", .{});
}
Summary
V aims for simplicity and ease of use, with a focus on rapid development and safety. It offers a more approachable syntax and built-in features like package management. However, it lacks the maturity and low-level control that Zig provides.
Zig, on the other hand, emphasizes low-level control and performance, making it more suitable for systems programming and embedded development. It has a larger community and ecosystem, but comes with a steeper learning curve.
Both languages have their strengths and are designed for different use cases. V is better suited for application development, while Zig excels in systems programming and performance-critical scenarios.
The Crystal Programming Language
Pros of Crystal
- Ruby-like syntax, making it easier for Ruby developers to transition
- Built-in type inference, reducing the need for explicit type annotations
- Macros for metaprogramming, enabling powerful code generation
Cons of Crystal
- Smaller community and ecosystem compared to Zig
- Longer compilation times, especially for large projects
- Less focus on low-level control and systems programming
Code Comparison
Crystal:
def fibonacci(n)
return n if n <= 1
fibonacci(n - 1) + fibonacci(n - 2)
end
puts fibonacci(10)
Zig:
fn fibonacci(n: u64) u64 {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
pub fn main() void {
std.debug.print("{}\n", .{fibonacci(10)});
}
Key Differences
- Crystal aims for Ruby-like simplicity, while Zig focuses on low-level control
- Crystal uses garbage collection, whereas Zig provides manual memory management
- Zig offers better cross-compilation and supports more platforms
- Crystal has more expressive metaprogramming capabilities
- Zig emphasizes compile-time execution and safety checks
Both languages aim to provide performance and safety, but Zig leans more towards systems programming, while Crystal targets a balance between productivity and performance.
Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
Pros of Nim
- More mature and established language with a larger ecosystem
- Supports multiple programming paradigms (imperative, OOP, functional)
- Easier learning curve for developers coming from Python or JavaScript
Cons of Nim
- Slower compilation times compared to Zig
- Less focus on low-level control and systems programming
- Garbage collection by default, which may not be ideal for some use cases
Code Comparison
Nim:
proc fibonacci(n: int): int =
if n < 2:
result = n
else:
result = fibonacci(n - 1) + fibonacci(n - 2)
echo fibonacci(10)
Zig:
fn fibonacci(n: u64) u64 {
if (n < 2) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
pub fn main() void {
std.debug.print("{}\n", .{fibonacci(10)});
}
Both Nim and Zig aim to be modern, efficient programming languages, but they have different focuses. Nim emphasizes productivity and readability, while Zig prioritizes low-level control and performance. Nim's syntax is more familiar to Python developers, whereas Zig's syntax is closer to C. Zig offers better memory safety guarantees without garbage collection, making it more suitable for systems programming. Nim, on the other hand, provides more flexibility in terms of programming paradigms and may be easier to adopt for general-purpose programming tasks.
The Kotlin Programming Language.
Pros of Kotlin
- More mature ecosystem with extensive libraries and frameworks
- Seamless interoperability with Java, allowing gradual adoption
- Concise syntax and powerful features like null safety and coroutines
Cons of Kotlin
- Slower compilation times compared to Zig's fast compilation
- Larger runtime overhead due to JVM dependency
- Less control over low-level system resources
Code Comparison
Kotlin:
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = numbers.map { it * 2 }
println(doubled)
}
Zig:
const std = @import("std");
pub fn main() !void {
const numbers = [_]i32{ 1, 2, 3, 4, 5 };
var doubled = try std.ArrayList(i32).initCapacity(std.heap.page_allocator, numbers.len);
defer doubled.deinit();
for (numbers) |num| {
try doubled.append(num * 2);
}
std.debug.print("{any}\n", .{doubled.items});
}
The Kotlin example showcases its concise syntax and functional programming features, while the Zig example demonstrates manual memory management and explicit error handling.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
A general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
Documentation
If you are looking at this README file in a source tree, please refer to the Release Notes, Language Reference, or Standard Library Documentation corresponding to the version of Zig that you are using by following the appropriate link on the download page.
Otherwise, you're looking at a release of Zig, so you can find the language
reference at doc/langref.html
, and the standard library documentation by
running zig std
, which will open a browser tab.
Installation
A Zig installation is composed of two things:
- The Zig executable
- The lib/ directory
At runtime, the executable searches up the file system for the lib/ directory, relative to itself:
- lib/
- lib/zig/
- ../lib/
- ../lib/zig/
- (and so on)
In other words, you can unpack a release of Zig anywhere, and then begin
using it immediately. There is no need to install it globally, although this
mechanism supports that use case too (i.e. /usr/bin/zig
and /usr/lib/zig/
).
Building from Source
Ensure you have the required dependencies:
- CMake >= 3.15
- System C/C++ Toolchain
- LLVM, Clang, LLD development libraries == 19.x
Then it is the standard CMake build process:
mkdir build
cd build
cmake ..
make install
For more options, tips, and troubleshooting, please see the Building Zig From Source page on the wiki.
Building from Source without LLVM
In this case, the only system dependency is a C compiler.
cc -o bootstrap bootstrap.c
./bootstrap
This produces a zig2
executable in the current working directory. This is a
"stage2" build of the compiler,
without LLVM extensions, and is
therefore lacking these features:
- Release mode optimizations
- aarch64 machine code backend
- @cImport
- zig translate-c
- Ability to compile assembly files
- Some ELF linking features
- Most COFF/PE linking features
- Some WebAssembly linking features
- Ability to create import libs from def files
- Ability to create static archives from object files
- Ability to compile C, C++, Objective-C, and Objective-C++ files
However, a compiler built this way does provide a C backend, which may be useful for creating system packages of Zig projects using the system C toolchain. In this case, LLVM is not needed!
Furthermore, a compiler built this way provides an LLVM backend that produces bitcode files, which may be compiled into object files via a system Clang package. This can be used to produce system packages of Zig applications without the Zig package dependency on LLVM.
Contributing
Zig is Free and Open Source Software. We welcome bug reports and patches from everyone. However, keep in mind that Zig governance is BDFN (Benevolent Dictator For Now) which means that Andrew Kelley has final say on the design and implementation of everything.
One of the best ways you can contribute to Zig is to start using it for an open-source personal project.
This leads to discovering bugs and helps flesh out use cases, which lead to further design iterations of Zig. Importantly, each issue found this way comes with real world motivations, making it straightforward to explain the reasoning behind proposals and feature requests.
You will be taken much more seriously on the issue tracker if you have a personal project that uses Zig.
The issue label Contributor Friendly exists to help you find issues that are limited in scope and/or knowledge of Zig internals.
Please note that issues labeled Proposal but do not also have the Accepted label are still under consideration, and efforts to implement such a proposal have a high risk of being wasted. If you are interested in a proposal which is still under consideration, please express your interest in the issue tracker, providing extra insights and considerations that others have not yet expressed. The most highly regarded argument in such a discussion is a real world use case.
For more tips, please see the Contributing page on the wiki.
Community
The Zig community is decentralized. Anyone is free to start and maintain their own space for Zig users to gather. There is no concept of "official" or "unofficial". Each gathering place has its own moderators and rules. Users are encouraged to be aware of the social structures of the spaces they inhabit, and work purposefully to facilitate spaces that align with their values.
Please see the Community wiki page for a public listing of social spaces.
Top Related Projects
Empowering everyone to build reliable and efficient software.
The Go programming language
Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
The Crystal Programming Language
Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
The Kotlin Programming Language.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot