Content deleted Content added
m →top: Minor grammar edit to the beginning of the page Tags: Mobile edit Mobile app edit Android app edit App full source |
Rescuing 2 sources and tagging 0 as dead.) #IABot (v2.0.9.5 |
||
(30 intermediate revisions by 18 users not shown) | |||
Line 11:
| latest preview version =
| latest preview date =
| typing = [[Static typing|Static]], [[Strong and weak typing|strong]], [[Type inference|inferred]], [[
| memory management = [[Manual memory management|Manual]]
| platform = [[x86-64]], [[AArch64|ARM64]], [[WebAssembly]]<br/>Tier 2: [[ARM architecture|ARM]], [[IA-32]], [[RISC-V]], [[MIPS architecture|MIPS64]], [[Power ISA|POWERPC64]], [[SPARC|SPARC64]], some tier-2 platforms have tier-1 support for ''[[standalone program]]s''
| operating system = [[Cross-platform software|Cross-platform]]: [[Linux]], [[macOS]], [[FreeBSD]], [[Microsoft Windows|Windows]]
Line 28 ⟶ 29:
}}
'''Zig'''
A major goal of the language is to improve on the [[C (programming language)|C language]]
The main drawback of the system is that, although Zig has a growing community, as of 2025, it remains a new language with areas for improvement in maturity, ecosystem and tooling.<ref name="logrocket">{{cite web |last=Chigozie |first=Oduah |title=Comparing Rust vs. Zig: Performance, Safety, and More |url=https://blog.logrocket.com/comparing-rust-vs-zig-performance-safety-more/ |website=LogRocket Blog |date=2024-06-04 |access-date=2024-07-16}}</ref> Also the learning curve for Zig can be steep, especially for those unfamiliar with low-level programming concepts.<ref name="logrocket"/> The availability of learning resources is limited for complex use cases, though this is gradually improving as interest and adoption increase.<ref name="logrocket"/> Other challenges mentioned by the reviewers are interoperability with other languages (extra effort to manage data marshaling and communication is required), as well as manual memory deallocation (disregarding proper memory management results directly in memory leaks).<ref name="logrocket"/>
The development is funded by the Zig Software Foundation (ZSF), a non-profit corporation with Andrew Kelley as president, which accepts donations and hires multiple full-time employees.<ref>{{Cite web |title=Jakub Konka on Twitter |url=https://twitter.com/kubkon/status/1377146321136537602 |archive-url=https://web.archive.org/web/20220410102319/https://twitter.com/kubkon/status/1377146321136537602 |archive-date=2022-04-10 |access-date=2021-05-28 |website=Twitter |language=en}}</ref><ref>{{Cite web|title=Announcing the Zig Software Foundation|url=https://ziglang.org/news/announcing-zig-software-foundation/|access-date=2021-05-28|website=Ziglang.org}}</ref><ref>{{Cite web|title=Sponsor ZSF|url=https://ziglang.org/zsf/|access-date=2021-05-28|website=Ziglang.org}}</ref> Zig has very active contributor community, and is still in its early stages of development.<ref name="TNW1">{{cite web |url = https://thenextweb.com/news/zig-highest-paying-programming-language |title = Why Zig has become the highest-paying programming language |last =Kavanagh |first =Amanda |date = June 25,
==Language==
Line 42 ⟶ 43:
In keeping with the overall design philosophy of making the code simple and easy to read, the Zig system as a whole also encompasses a number of stylistic changes compared to C and other C-like languages. For instance, the [[Rust (programming language)|Rust]] language has [[operator overloading]] which means a statement like {{code|a {{=}} b + c}} might actually be a function call to a type’s overloaded version of the plus operator. Further, that function might panic which might pre-empt any following code. 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,{{sfn|Yegulalp|2016}} error handling is handled through error types and can be handled with {{code|catch}} or {{code|try}}.
The goals of Zig are in contrast to those of many other languages designed in the same time period, like
===Memory handling===
Line 49 ⟶ 50:
A common solution to these problems is a [[Garbage collection (computer science)|garbage 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, GC systems are relatively slow compared to manual memory management{{citation needed|date=August 2024}}, and have unpredictable performance that makes them unsuited to [[systems programming]]. Another solution is [[automatic 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.<ref name=gc>{{cite web |url=https://docs.elementscompiler.com/Concepts/ARCvsGC/ |title=ARC vs. GC |website=Elements}}</ref>
Zig aims to provide performance similar to or better than C, so GC and ARC are not suitable solutions. Instead, it uses a modern,{{citation needed|date=May 2025}} {{as of|2022|lc=yes}}, concept known as [[option type]]s. 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.<ref name=javao>{{cite web |url=https://www.baeldung.com/java-optional |title= Guide To Java 8 Optional |date=28 November 2022}}</ref>
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.<ref name=rusto>{{cite web |url=https://ggbaker.ca/prog-langs/content/rust-memory.html |title= Rust: Memory Management}}</ref>
Line 60 ⟶ 61:
In the code, the function would examine the size of {{code|original}} and then malloc {{code|times}} 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:
<syntaxhighlight lang="zig">
fn repeat(allocator:
</syntaxhighlight>
In this code, the {{code|allocator}} variable is passed a struct that describes what code should perform the allocation, and the {{code|repeat}} function returns either the resulting string or, using the optional type as indicated by the {{code|!}}, 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’s [[standard 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 the [[operating system]] functions that normally allocate an entire [[memory page]].<ref name=zigo>{{cite web |url=https://ziglearn.org/chapter-2/ |title=Allocators|date=11 September 2023 }}</ref>
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
Another more general feature of Zig that also helps manage memory problems is the concept of {{code|defer}}, which marks some code to be performed at the end of a function 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 a {{code|free}} to ensure it is released no matter what happens.<ref name=zigo/>
Line 98 ⟶ 99:
==Compiler==
Zig
Zig treats cross-compiling as a first-class use-case of the language.<ref name=zig /> 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 like [[ARM architecture family|ARM]] and [[x86-64]], but also [[PowerPC]], [[SPARC]], [[MIPS architecture|MIPS]], [[RISC-V]], [[LoongArch64]] and even the IBM [[z/Architecture]]s (S390). The toolchain can compile to any of these targets without installing additional software, all the needed support is in the basic system.<ref name=zigo/> 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).
Line 105 ⟶ 106:
Zig uses [[LLVM]] (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 a [[self-hosting compiler]]. The self-hosted linker is tightly coupled with the self-hosted compiler.
The LLVM backend is the default for most targets, except for [[Standard Portable Intermediate Representation|SPIR-V]]. Zig also supports their self-hosted backend which can be enabled by using <code>-fno-llvm</code>.
==Packages==
Version 0.11.0 bundles an experimental [[package manager]], but no official [[software repository|package repository]] is available. Instead a package is simply a URL that points to a [[Compressed file library|compressed file]], or a [[Git]] [[Repository (version control)|repository]]. Each package ideally includes a standard
==Examples==
Line 115 ⟶ 118:
const std = @import("std");
pub fn main()
▲ try stdout.print("Hello, {s}!\n", .{"world"});
}
</syntaxhighlight>
Line 198 ⟶ 201:
fn repeat(
allocator:
original: []const u8,
times: usize,
Line 226 ⟶ 229:
defer arena.deinit();
const original = "Hello ";
const repeated = try repeat(
original,
3,
Line 248 ⟶ 251:
The previous [[Bootstrapping (compilers)|bootstrapping]] compiler, written in Zig and C++ using [[LLVM]] as a back-end,<ref>{{Cite web|url=https://www.gingerbill.org/article/2019/05/13/a-reply-to-the-road-to-zig/|title=A Reply to _The Road to Zig 1.0_|date=2019-05-13|website=www.gingerbill.org|language=en-gb|access-date=2020-02-11}}</ref><ref>{{Cite web|title=ziglang/zig|date=2020-02-11|url=https://github.com/ziglang/zig|website=GitHub|publisher=Zig Programming Language|access-date=2020-02-11}}</ref> supporting many of its native targets,<ref>{{Cite web|url=https://ziglang.org/#Tier-System|title=The Zig Programming Language|website=Ziglang.org|access-date=2020-02-11}}</ref> was removed in version 0.11. Newer versions of Zig use a prebuilt [[WebAssembly]] version of Zig to bootstrap itself.
=== Versions ===
All releases, along with their release dates and pre-built platforms. <ref>{{Cite web|title=Download ⚡ Zig Programming Language|url=https://ziglang.org/download/|access-date=2025-05-06|website=ziglang.org}}</ref>
{|class="wikitable"
!rowspan="2"|Name!!rowspan="2"|Date!!colspan="3"|Windows!!colspan="9"|Linux!!colspan="2"|macOS!!FreeBSD
|-
!x86_64!!x86!!aarch64!!x86_64!!x86!!aarch64!!armv6kz!!arm7a!!riscv64!!powerpc64le!!powerpc!!loongarch64!!x86_64!!aarch64!!x86_64
|-
|0.1.1||2017 Q4||{{ya}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}
|-
|0.2.0||2018 Q1||{{ya}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}
|-
|0.3.0||2018 Q3||{{ya}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{na}}
|-
|0.4.0||2019 Q2||{{ya}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{ya}}
|-
|0.5.0||2019 Q3||{{ya}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{ya}}
|-
|0.6.0||2020 Q2||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{na}}
|-
|0.7.0||2020 Q4||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.7.1||2020 Q4||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{ya}}
|-
|0.8.0||2021 Q3||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.8.1||2021 Q3||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.9.0||2021 Q4||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.9.1||2022 Q1||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.10.0||2022 Q4||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.10.1||2023 Q1||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{ya}}||{{na}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{na}}
|-
|0.11.0||2023 Q3||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.12.0||2024 Q2||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.12.1||2024 Q3||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.13.0||2024 Q3||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{na}}||{{ya}}||{{ya}}||{{ya}}
|-
|0.14.0||2025 Q1||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}{{Efn|This version of prebuilt binary is broken due to a regression in the LoongArch backend of LLVM.<ref>{{Cite web |title=Zig crashes on LoongArch64 · Issue #22859 · ziglang/zig |url=https://github.com/ziglang/zig/issues/22859 |access-date=2025-06-20 |website=GitHub |language=en}}</ref>|name=loongarch0.14}}||{{ya}}||{{ya}}||{{na}}
|-
|0.14.1||2025 Q2||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}||{{ya}}||{{ya}}||{{na}}||{{ya}}{{Efn|name=loongarch0.14}}||{{ya}}||{{ya}}||{{na}}
|-
|}
{{Notelist}}
== Projects ==
Line 254 ⟶ 307:
* The TigerBeetle<ref>{{Cite web |title=tigerbeetle/tigerbeetle |url= https://github.com/tigerbeetle/tigerbeetle |access-date=2024-12-30 |website=Github |language=en}}</ref> financial transaction database is written in Zig.
* [https://github.com/Senryoku/Deecy Deecy], an up-and-coming (latest release as of this writing is version 0.3.0) [[emulator]] of [[Sega|Sega's]] [[Dreamcast]] home video gaming console platform, completely written in Zig from scratch.
==See also==
{{Portal|Free and open-source software|Computer programming}}
▲*[[C (programming language)|C]]
*[[D (programming language)|D]]
*[[V (programming language)|V]]
Line 314 ⟶ 360:
*[https://www.youtube.com/watch?v=Z4oYSByyRak Movie: Introducing Zig]
*[https://www.youtube.com/watch?v=Gv2I7qTux7g Movie: The Road to 1.0]
*[https://discu.eu/weekly/zig/ Zig Weekly] {{Webarchive|url=https://web.archive.org/web/20250114161926/https://discu.eu/weekly/zig/ |date=2025-01-14 }}
{{Programming languages}}
Line 331 ⟶ 377:
[[Category:Statically typed programming languages]]
[[Category:Systems programming languages]]
[[Category:Compiled programming languages]]
|