Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Zig (programming language)

From Wikipedia, the free encyclopedia
General-purpose programming language
This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages)
icon
This articlerelies excessively onreferences toprimary sources. Please improve this article by addingsecondary or tertiary sources.
Find sources: "Zig" programming language – news ·newspapers ·books ·scholar ·JSTOR
(October 2025) (Learn how and when to remove this message)
Some of this article'slisted sourcesmay not bereliable. Please help improve this article by looking for better, more reliable sources. Unreliable citations may be challenged and removed.(October 2025) (Learn how and when to remove this message)
This articlecontainspromotional content. Please helpimprove it by removingpromotional language and inappropriateexternal links, and by adding encyclopedic text written from aneutral point of view.
See ouradvice if the article is about you and read ourscam warning in case someone asks for money to edit this article.
(December 2025) (Learn how and when to remove this message)
(Learn how and when to remove this message)

Zig
ParadigmsMulti-paradigm:imperative,concurrent,procedural,functional
Designed byAndrew Kelley
First appeared8 February 2016; 10 years ago (2016-02-08)[1]
Stable release
0.15.2[2] Edit this on Wikidata (Beta) / 11 October 2025; 4 months ago (11 October 2025)
Typing disciplineStatic,strong,inferred,nominal,generic
Memory managementManual
Implementation languageZig
Platformx86-64,ARM64,WebAssembly
Tier 2:ARM,IA-32,RISC-V,MIPS64,POWERPC64,SPARC64, some tier-2 platforms have tier-1 support forstandalone programs
OSCross-platform:Linux,macOS,FreeBSD,Windows
LicenseMIT
Filename extensions.zig, .zir, .zigr, .zon
Websiteziglang.org
Influenced by
C,C++,LLVM IR,Go,Rust[3]

[4][5][6][7][8][9]

[10]

Zig is asystem programming language designed to be a general-purpose improvement to theC programming language.[11] It isfree and open-source software, released under anMIT License.[12]

Differences with C relate tocontrol flow,function calls,library imports,variable declaration andUnicode support. The language makes no use ofmacros orpreprocessor instructions. Features adopted from modern languages include the addition ofcompile timegeneric programmingdata types, allowing functions to work on a variety of data, along with a small set of newcompiler directives to allow access to the information about those types usingreflection.[13] Zig requiresmanual memory management,[13] but attempts to improvememory safety throughoption types and aunit testing framework. Features forlow-level programming include packedstructs, arbitrary-widthintegers[14] and multiplepointer types.[15]

Zig was designed by Andrew Kelly and first announced in 2016.[1] Development is funded by the Zig Software Foundation (ZSF).[16]

Language

[edit]

Goals

[edit]

The primary goal of Zig is to be a better solution to the sorts of tasks that are currently solved with C. A primary concern in that respect is readability; Zig attempts to use existing concepts and syntax wherever possible, avoiding the addition of different syntax for similar concepts. Further, it is designed for "robustness, optimality and maintainability", including a variety of features to improve safety, optimization, and testing. The small and simple syntax is an important part of the maintenance, as it is a goal of the language to allow maintainers to debug the code without having to learn the intricacies of a language they might not be familiar with.[17] Even with these changes, Zig can compile into and against existing C code; C headers can be included in a Zig project and their functions called, and Zig code can be linked into C projects by including the compiler-built headers.[18]

In Zig, if something calls a function, it looks like a function call; if it doesn’t, it doesn’t look like a function call. If it can raise an error, it is explicit in the syntax.[18] Error handling is handled through error types and can be handled withcatch ortry. Zig has a more conservative extension of the type system, supporting compile time generics and accommodating a form ofduck typing with thecomptime directive.

Memory handling

[edit]

One of the primary sources ofbugs in C programs is thememory management system, based onmalloc. malloc sets aside a block of memory for use in the code and returns a reference to that memory as apointer. There is no system to ensure that memory is released when the program no longer needs it, which can lead to programs using up all available memory, amemory leak. More common is adangling pointer that does not refer to a properly allocated memory object.[19]

A common solution to these problems is agarbage collector (GC), which examines the program for pointers to previously allocated memory, and removing any blocks that no longer have anything pointing to them. Although this greatly reduces, or even eliminates, memory errors, types of GC systems may have unpredictable performance that makes them unsuited toreal-time computing andsystems programming. Another solution isautomatic reference counting (ARC), which implements the same basic concept of identifying blocks of disused memory, but does so at pointer creation and destruction time by maintaining the number of pointers to a block, meaning there is no need to perform exhaustive pointer searches, which are rendered unnecessary at the cost of adding reference counter adjustment overhead to every pointer creation and destruction operation.[19]

Zig aims to provide performance similar to or better than C, so GC and ARC are not suitable solutions. Instead, it uses a concept known asoption types. Instead of a pointer being allowed to point to nothing, or nil, a separate type is used to indicate data that is optionally empty. This is similar to using a structure with a pointer and a boolean that indicates whether the pointer is valid, but the state of the boolean is invisibly managed by the language and does not need to be explicitly managed by the programmer. So, for instance, when the pointer is declared it is set to "unallocated", and when that pointer receives a value from a malloc, it is set to "allocated" if the malloc succeeded.[20]

The advantage to this model is that it has very low or zero overhead; the compiler has to create the code to pass along the optional type when pointers are manipulated, as opposed to a simple pointer, but this allows it to directly express possible memory problems at compile time with no runtime support. For instance, creating a pointer with a null value and then attempting to use it is perfectly acceptable in C, leading to null-pointer errors. In contrast, a language using optional types can check that all code paths only attempt to use pointers when they are valid. While this does not eliminate all potential problems, when issues do occur at runtime the error can be more precisely located and explained.[21]

Another change for memory management in Zig is that the actual allocation is handled throughstructs describing the action, as opposed to calling the memory management functions inlibc. For instance, in C if one wants to write a function that makes a string containing multiple copies of another string, the function might look like this:[citation needed]

constchar*repeat(constchar*original,size_ttimes);

In the code, the function would examine the size oforiginal and then malloctimes that length to set aside memory for the string it will build. That malloc is invisible to the functions calling it, if they fail to later release the memory, a leak will occur. In Zig, this might be handled using a function like:[citation needed]

constAllocator=std.mem.Allocator;fnrepeat(allocator:Allocator,original:[]constu8,times:usize)Allocator.Error![]constu8;

In this code, theallocator variable is passed a struct that describes what code should perform the allocation, and therepeat function returns either the resulting string or, using the optional type as indicated by the!, an Allocator.Error. By directly expressing the allocator as an input, memory allocation is never "hidden" within another function, it is always exposed to the API by the function that is ultimately calling for the memory to be allocated. No allocations are performed inside Zig’sstandard library. Further, as the struct can point to anything, one can use alternative allocators, even ones written in the program. This can allow, for instance, small-object allocators that do not use theoperating system functions that normally allocate an entirememory page.[22]

Optional types are an example of a language feature that offers general functionality while still being simple and generic. They do not have to be used to solve null pointer problems; they are also useful for any type of value where "no value" is an appropriate answer. Consider a functioncountTheNumberOfUsers that returns an integer, and an integer variable,theCountedUsers that holds the result. In many languages, amagic number would be placed intheCountedUsers to indicate thatcountTheNumberOfUsers has not yet been called, while many implementations would just set it to zero. In Zig, this could be implemented as anvartheCountedUsers:?i32=null which sets the variable to a clear "not been called" value.[22]

Another more general feature of Zig that also helps manage memory problems is the concept ofdefer, which marks some code to be performed at the end of a scope no matter what happens, including possible runtime errors. If a particular function allocates some memory and then disposes of it when the operation is complete, one can add a line to defer afree to ensure it is released no matter what happens.[22]

Zig memory management avoids hidden allocations. Allocation is not managed in the language directly. Instead, heap access is done via thestandard library, explicitly.[23]

Direct interaction with C

[edit]

Zig promotes a gradual approach to portability that combines new Zig code with existing C code. To do this, it aims to make interaction with existing C libraries as seamless as possible. Zig imports its own libraries with the@import directive, typically in this fashion:

conststd=@import("std");

Zig code within that file can now call functions inside std, for instance:

std.debug.print("Hello, world!\n",.{});

To work with C code, one simply replaces the@import with@cImport:

constc=@cImport(@cInclude("soundio/soundio.h"));

The Zig code can now call functions in the soundio library as if they were native Zig code. As Zig uses new data types that are explicitly defined, unlike C’s more genericint andfloat, a small number of directives are used to move data between the C and Zig types, including@intCast and@ptrCast.[22]

Comptime

[edit]

By using thecomptime keyword, the programmer can explicitly have Zig evaluate sections of code atcompile time, as opposed toruntime. Being able to run code at compile time allows Zig to have the functionality ofmacros andconditional compilation without the need for a separatepreprocessor language.[24]

During compile time, types becomefirst-class citizens. This enables compile-timeduck typing, and is how Zig implements generic types.[15]

For instance, in Zig, a genericlinked list type might be implemented using a function like:

fnLinkedList(comptimeT:type)type;

This function takes in some typeT, and returns a customstruct defining a linked list with that data type.

Compiler

[edit]

Zig also includes a C and C++ compiler, and can be used with either or both languages by leveraging with the commandszig cc andzig c++,[25] providing many headers including theC standard library (libc) andC++ Standard Library (libcxx) for many different platforms. This allows Zig’scc andc++ sub-commands to act ascross compilers out of the box (similarly toClang).[26][27]

Zig treats cross-compiling as a first-class use-case of the language.[13] This means any Zig compiler can compile runnable binaries for any of its target platforms, of which there are dozens. These include not only widely-used modern systems likeARM andx86-64, but alsoPowerPC,SPARC,MIPS,RISC-V,LoongArch64 and even the IBMz/Architectures (S390). The toolchain can compile to any of these targets without installing additional software, all the needed support is in the basic system.[22] The experimental support is also provided for less known platforms like AMD and Nvidia GPUs or PlayStation 4 and 5 (with various degree of support).

Cross-compilation is also available for variety of the operating systems (mostly desktop ones). Popular UNIX-like ones and Windows are officially supported (and documented), but (minimal) applications can and have been made for Android (withAndroid NDK) oriOS.[citation needed]

Zig usesLLVM (written in C++) as a backend for optimization. Since version 0.10 the Zig compiler is written in the Zig programming language, i.e., it is aself-hosting compiler. The self-hosted linker is tightly coupled with the self-hosted compiler.[citation needed]

TheLLVM backend is the default for most targets, except forSPIR-V, andx86-64[28] (although this is currently just in Debug mode). Zig also supports their self-hosted backend which can be enabled by using-fno-llvm.

History

[edit]

The nameZig was picked using a script that generated random combinations of letters starting with the letterz.[29]

The previousbootstrapping compiler, written in Zig and C++ usingLLVM as a back-end,[30][31] supporting many of its native targets,[32] was removed in version 0.11. Newer versions of Zig use a prebuiltWebAssembly version of Zig to bootstrap itself.

On 26 November 2025, Zig development migrated fromGitHub toCodeberg.[33]

Packages

[edit]

Version 0.11.0 bundles an experimentalpackage manager, but no officialpackage repository is available. Instead a package is simply a URL that points to acompressed file, or aGitrepository. Each package ideally includes a standardbuild.zig file (that the Zig compiler uses by convention to compile the source code) and abuild.zig.zon file containing metadata with name and version of the package.[citation needed]

Examples

[edit]

Hello World

[edit]
conststd=@import("std");constFile=std.fs.File;pubfnmain()!void{_=tryFile.stdout().write("Hello, World!\n");}

Generic linked list

[edit]
conststd=@import("std");constFormatOptions=std.fmt.FormatOptions;conststdout=std.io.getStdOut().writer();fnLinkedList(comptimeT:type)type{returnstruct{constSelf=@This();pubconstNode=struct{next:?*Node=null,data:T,};first:?*Node=null,pubfnprepend(list:*Self,new_node:*Node,)void{new_node.next=list.first;list.first=new_node;}pubfnformat(list:Self,comptimefmt:[]constu8,options:FormatOptions,out_stream:anytype,)!void{tryout_stream.writeAll("( ");varit=list.first;while(it)|node|:(it=node.next){trystd.fmt.formatType(node.data,fmt,options,out_stream,1,);tryout_stream.writeAll(" ");}tryout_stream.writeAll(")");}};}pubfnmain()!void{constListU32=LinkedList(u32);varlist=ListU32{};varnode1=ListU32.Node{.data=1};varnode2=ListU32.Node{.data=2};varnode3=ListU32.Node{.data=3};list.prepend(&node1);list.prepend(&node2);list.prepend(&node3);trystdout.print("{}\n",.{list});trystdout.print("{b}\n",.{list});}

Output:

( 3 2 1 )( 11 10 1 )

String repetition with allocator

[edit]
conststd=@import("std");constArenaAllocator=std.heap.ArenaAllocator;constAllocator=std.mem.Allocator;fnrepeat(allocator:Allocator,original:[]constu8,times:usize,)Allocator.Error![]u8{constbuffer=tryallocator.alloc(u8,original.len*times,);for(0..times)|i|{std.mem.copyForwards(u8,buffer[(original.len*i)..],original,);}returnbuffer;}pubfnmain()!void{vararena=ArenaAllocator.init(std.heap.page_allocator,);deferarena.deinit();constallocator=arena.allocator();constoriginal="Hello ";constrepeated=tryrepeat(allocator,original,3,);std.debug.print("{s}\n",.{repeated});}

Output:

Hello Hello Hello

Notable projects

[edit]

Projects that use Zig include:

References

[edit]

Citations

[edit]
  1. ^abKelley, Andrew (8 February 2016)."Introduction to the Zig Programming Language".andrewkelley.me. Retrieved8 November 2020.
  2. ^"Release 0.15.2". Retrieved27 October 2025.
  3. ^"What are the pros and cons of Zig vs Rust? I see Zig mentioned more and more her... | Hacker News".
  4. ^"Why Zig when There is Already C++, D, and Rust? ⚡ Zig Programming Language".
  5. ^"No surprises on any system: Q&A with Loris Cro of Zig - Stack Overflow". 2 October 2023.
  6. ^"Zig's New Relationship with LLVM | Hacker News".
  7. ^"What's Zig got that C, Rust and Go don't have? (With Loris Cro)".YouTube. 15 November 2023.
  8. ^"Why Zig when there is already C++, D, and Rust? | Hacker News".
  9. ^"Overview ⚡ Zig Programming Language".
  10. ^"After a day of programming in Zig". 29 December 2023.
  11. ^"Taking the warts off C, with Andrew Kelley, creator of the Zig programming language".Sourcegraph. 19 October 2021. Archived fromthe original on 5 January 2025. Retrieved18 April 2024.
  12. ^"ziglang/zig".GitHub. Retrieved11 February 2020.
  13. ^abc"The Zig Programming Language".Ziglang.org. Retrieved11 February 2020.
  14. ^Anderson, Tim (24 April 2020)."Keen to go _ExtInt? LLVM Clang compiler adds support for custom width integers".www.theregister.co.uk. Retrieved30 December 2024.
  15. ^ab"Documentation".Ziglang.org. Retrieved24 April 2020.
  16. ^"Announcing the Zig Software Foundation".Ziglang.org. Retrieved28 May 2021.
  17. ^Elizabeth 2017.
  18. ^abYegulalp 2016.
  19. ^ab"ARC vs. GC".Elements.
  20. ^"Guide To Java 8 Optional". 28 November 2022.
  21. ^"Rust: Memory Management".
  22. ^abcde"Allocators". 11 September 2023.
  23. ^Tyson, Matthew (9 March 2023)."Meet Zig: The modern alternative to C".InfoWorld.com.
  24. ^The Road to Zig 1.0 - Andrew Kelley. ChariotSolutions. 9 May 2019 – viaYouTube.
  25. ^"0.6.0 Release Notes".Ziglang.org. Retrieved19 April 2020.
  26. ^"'zig cc': a Powerful Drop-In Replacement for GCC/Clang - Andrew Kelley".andrewkelley.me. Retrieved28 May 2021.
  27. ^"Zig Makes Go Cross Compilation Just Work".DEV Community. 24 January 2021. Retrieved28 May 2021.
  28. ^"0.15.1 Release Notes ⚡ The Zig Programming Language".ziglang.org. Retrieved3 September 2025.
  29. ^andrewrk (13 March 2024)."origin of the zig programming language name. by @andrewrk". Retrieved13 March 2024.
  30. ^"A Reply to _The Road to Zig 1.0_".www.gingerbill.org. 13 May 2019. Retrieved11 February 2020.
  31. ^"ziglang/zig".GitHub. Zig Programming Language. 11 February 2020. Retrieved11 February 2020.
  32. ^"The Zig Programming Language".Ziglang.org. Retrieved11 February 2020.
  33. ^Kelley, Andrew (26 November 2025)."Migrating from GitHub to Codeberg".ziglang.org.Archived from the original on 27 November 2025. Retrieved27 November 2025.
  34. ^Tyson, Matthew (23 February 2023)."Explore Bun.js: The all-in-one JavaScript runtime".InfoWorld.Archived from the original on 4 October 2023. Retrieved11 October 2023.
  35. ^Greef, Joran Dirk (4 August 2023)."A New Era for Database Design with TigerBeetle".InfoQ. Retrieved28 January 2026.
  36. ^tigerbeetle/tigerbeetle, TigerBeetle, 28 January 2026, retrieved28 January 2026
  37. ^Proven, Liam (8 January 2025)."Just when you thought terminal emulators couldn't get any better, Ghostty ships".The Register.

Bibliography

[edit]

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Zig_(programming_language)&oldid=1337844548"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp