Zig (programming language)

Zig
Zig logo 2020.svg
ParadigmsMulti-paradigm: imperative, concurrent, procedural, functional
Designed byAndrew Kelley
First appeared8 February 2016; 6 years ago (2016-02-08)[1]
Preview release
0.10.1[2] Edit this on Wikidata / 19 January 2023; 14 days ago (19 January 2023)
Typing disciplineStatic, strong, inferred, structural, generic
Platformx86-64, ARM64, WebAssembly
Tier 2: ARM, IA-32, RISC-V, MIPS64, POWERPC64, IA-32, SPARC64, some tier-2 platforms have tier-1 support when "freestanding"
OSCross-platform: Linux, FreeBSD, Windows
LicenseMIT
Filename extensions.zig, .zir
Websiteziglang.org
Influenced by
C, C++, LLVM IR, Go, Rust, JavaScript[citation needed]

Zig is an imperative, general-purpose, statically typed, compiled system programming language designed by Andrew Kelley.[3][4] The language is designed for "robustness, optimality and maintainability",[5][6] supporting compile-time generics, reflection and evaluation, cross-compilation, and manual memory management.[7] A major goal of the language is to improve upon the C language,[8][9] while also taking inspiration from Rust,[10][5] among others. Zig has many features for low-level programming, notably packed structs (structs without padding between fields), arbitrary-width integers[11] and multiple pointer types.[12]

Zig is not a new language only. It also includes a C/C++ compiler, and Zig can be used with either or both languages.

Since version 0.10 the (new default) Zig compiler is written in Zig, i.e., it is a self-hosting compiler, and that is a major new feature of that release (the older legacy bootstrapping compiler, written in C++, is still an option but will not be in 0.11). The default backend (i.e. the optimizer) is still LLVM (now version 15, legacy uses version 13), and LLVM is written in C++. Zig (i.e. the compiler, not generated code from it) with LLVM is 169 MiB, vs without LLVM 4.4 MiB. When compiling with the new Zig compiler much less memory is used (the older, now legacy, compiler uses 3.5x more memory), and it compiles a bit faster. Faster executable code is usually complied with the new compiler (i.e. its LLVM code generation is better), and it fixes many bugs, but there are also improvements for the older legacy compiler in version 0.10. The self-hosted linker is tightly coupled with the self-hosted compiler. The new version also adds some experimental (tier-3) support for AMD GPUs (there's also some lesser support for Nvidia GPUs and for PlayStation 4 and 5).

The older bootstrapping ("stage1") compiler is written in Zig and C++, using LLVM 13[13] as a back-end,[14][15] supporting many of its native targets.[16] The compiler is free and open-source software released under an MIT License.[17] The Zig compiler exposes the ability to compile C and C++ similarly to Clang with the commands "zig cc" and "zig c++",[18] providing many headers including libc and libcxx for many different platforms, allowing Zig's cc and c++ sub-commands to act as cross compilers out of the box.[19][20]

Plus the operating systems (mostly desktop ones) officially supported (and documented), (minimal) applications can and have been made for Android (with Android NDK, and programming for iOS also possible).

Zig doesn't have its own official package manager (non-official ones exist), but a standard one has a milestone for 0.12.

Zig development is funded by the Zig Software Foundation (ZSF), a non-profit corporation with Andrew Kelley as president, which takes in donations and hires multiple full-time employees.[21][22][23]

Examples

Hello World

const std = @import("std");

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

Generic linked list

pub fn main() void {
    var node = LinkedList(i32).Node {
        .prev = null,
        .next = null,
        .data = 1234,
    };

    var list = LinkedList(i32) {
        .first = &node,
        .last = &node,
        .len = 1,
    };
}

fn LinkedList(comptime T: type) type {
    return struct {
        pub const Node = struct {
            prev: ?*Node,
            next: ?*Node,
            data: T,
        };

        first: ?*Node,
        last:  ?*Node,
        len:   usize,
    };
}

Projects

See also

References

  1. ^ Kelley, Andrew. "Introduction to the Zig Programming Language". andrewkelley.me. Retrieved 8 November 2020.
  2. ^ https://ziglang.org/download/#release-0.10.1.
  3. ^ "Zig has all the elegant simplicity of C, minus all the ways to shoot yourself in the foot". JAXenter. 2017-10-31. Archived from the original on 2017-11-01. Retrieved 2020-02-11.
  4. ^ "Tired of C? New programming language Zig aims to be more pragmatic and readable". 2017-10-19. Archived from the original on 2020-10-01. Retrieved 2020-04-22.
  5. ^ a b Yegulalp, Serdar (2016-08-29). "New challenger joins Rust to topple C language". InfoWorld. Retrieved 2020-02-11.
  6. ^ "Zig language and C". Sina Corp. 2020-07-12. Retrieved 2020-08-12.
  7. ^ "The Zig Programming Language". ziglang.org. Retrieved 2020-02-11.
  8. ^ "Mozilla's Observatory, the Zig programming language, and uSens' VR/AR SDK—SD Times news digest: Aug. 29, 2016". SD Times. 2016-08-29. Retrieved 2020-02-11.
  9. ^ "The Zig Programming Language". ziglang.org. Retrieved 2020-02-11.
  10. ^ Company, Sudo Null. "Sudo Null - IT News for you". SudoNull. Retrieved 2020-02-11.
  11. ^ Tim Anderson 24 Apr 2020 at 09:50. "Keen to go _ExtInt? LLVM Clang compiler adds support for custom width integers". www.theregister.co.uk. Retrieved 2020-04-24.
  12. ^ "Documentation - The Zig Programming Language". ziglang.org. Retrieved 2020-04-24.
  13. ^ "SD Times news digest: C++20 concepts in Visual Studio 2010 version 16.3, Bootstrap to drop IE support, and Zig 0.60 released". SD Times. 2020-04-14. Retrieved 2020-04-19.
  14. ^ "A Reply to _The Road to Zig 1.0_". www.gingerbill.org. 2019-05-13. Retrieved 2020-02-11.
  15. ^ ziglang/zig, Zig Programming Language, 2020-02-11, retrieved 2020-02-11
  16. ^ "The Zig Programming Language". ziglang.org. Retrieved 2020-02-11.
  17. ^ "ziglang/zig". GitHub. Retrieved 2020-02-11.
  18. ^ "0.6.0 Release Notes · The Zig Programming Language". ziglang.org. Retrieved 2020-04-19.
  19. ^ "'zig cc': a Powerful Drop-In Replacement for GCC/Clang - Andrew Kelley". andrewkelley.me. Retrieved 2021-05-28.
  20. ^ "Zig Makes Go Cross Compilation Just Work". DEV Community. Retrieved 2021-05-28.
  21. ^ "Jakub Konka on Twitter". Twitter. Retrieved 2021-05-28.{{cite web}}: CS1 maint: url-status (link)
  22. ^ "Announcing the Zig Software Foundation ⚡ Zig Programming Language". ziglang.org. Retrieved 2021-05-28.
  23. ^ "Sponsor ZSF ⚡ Zig Programming Language". ziglang.org. Retrieved 2021-05-28.

External links