Content deleted Content added
→Projects: fix spacing |
No edit summary |
||
Line 21:
<ref>{{cite web | url=https://ziglang.org/learn/why_zig_rust_d_cpp/ | title=Why Zig when There is Already C++, D, and Rust? ⚡ Zig Programming Language }}</ref>
<ref>{{cite web | url=https://stackoverflow.blog/2023/10/02/no-surprises-on-any-system-q-and-a-with-loris-cro-of-zig/ | title=No surprises on any system: Q&A with Loris Cro of Zig - Stack Overflow | date=2 October 2023 }}</ref>
<ref>{{cite web | url=https://news.ycombinator.com/item?id=24615916 | title=
<ref>{{cite web | url=https://www.reddit.com/r/rust/comments/14zehei/zig_moving_away_from_llvm_and_llvm_ecosystem/ | title=Zig moving away from LLVM and LLVM ecosystem | date=14 July 2023 }}</ref>
<ref>{{cite web | url=https://www.youtube.com/watch?v=5_oqWE9otaE | title=
<ref>https://news.ycombinator.com/item?id=25797025</ref>
<ref>{{cite web | url=https://ziglang.org/learn/overview/ | title=Overview ⚡ Zig Programming Language }}</ref>
Line 30:
'''Zig''' (Also known as Ziglang)<ref>{{Cite web |title=Home ⚡ Zig Programming Language |url=https://ziglang.org/ |access-date=2024-12-31 |website=ziglang.org}}</ref> is an [[Imperative programming|imperative]], [[General-purpose programming language|general-purpose]], [[statically typed]], [[Compiled language|compiled]] [[System programming language|system]] [[programming language]] designed by Andrew Kelley.<ref>{{Cite web |date=2021-10-19 |title=Taking the warts off C, with Andrew Kelley, creator of the Zig programming language |url=https://sourcegraph.com/podcast/andrew-kelley |access-date=2024-04-18 |website=Sourcegraph |language=en}}</ref> It is intended as a successor to the language [[C (programming language)|C]], with the intent of being even smaller and simpler to program in, while offering more functionality.<ref>{{Cite web |date=2017-10-31 |title=Zig has all the elegant simplicity of C, minus all the ways to shoot yourself in the foot |url=https://jaxenter.com/zig-language-kelley-interview-138517.html |archive-url=https://web.archive.org/web/20171101231710/https://jaxenter.com/zig-language-kelley-interview-138517.html |archive-date=2017-11-01 |access-date=2020-02-11 |website=JAXenter |language=en-US}}</ref> It is [[free and open-source software]], released under an [[MIT License]].
The improvements in language simplicity relate to flow control, [[Function (computer programming)|function calls]], [[Library (computing)|library]] imports, [[Declaration (computer programming)|variable declaration]] and [[Unicode]] support. Further, the language makes no use of [[Macro (computer science)|macros]] or [[preprocessor]] instructions. Features adopted from modern languages include the addition of [[compile time]] [[generic programming]] [[data type]]s, allowing functions to work on a variety of data, along with a small set of new [[compiler]] directives to allow access to the information about those types using [[reflective programming]] (reflection).<ref name=zig>{{Cite web|url=https://ziglang.org/|title=The Zig Programming Language|website=Ziglang.org|access-date=2020-02-11}}</ref>
Another set of additions to Zig is intended to improve code safety. A major goal of the language is to improve on the [[C (programming language)|C language]],<ref name="roadtozig1.0" /><ref>{{Cite web|url=https://ziglang.org/#Zig-competes-with-C-instead-of-depending-on-it|title=The Zig Programming Language|website=Ziglang.org|access-date=2020-02-11}}</ref> while also taking inspiration from [[Rust (programming language)|Rust]],<ref>{{Cite web |title=Zig programming language |url=https://sudonull.com/post/3683-Zig-programming-language |access-date=2020-02-11 |website=SudoNull |language=en}}</ref>{{sfn|Yegulalp|2016}} among others. Like C, Zig omits [[Garbage collection (computer science)|garbage collection]], and has [[manual memory management]].<ref
Zig 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>▼
==Language==
===Goals===
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.{{sfn|Elizabeth|2017}} 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.{{sfn|Yegulalp|2016}}
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
The goals of Zig are in contrast to those of many other languages designed in the same time period, like [[Go (programming language)|Go]], [[Rust (programming language)|Rust]], [[Carbon (programming language)|Carbon]], and [[Nim (programming language)|Nim]]. Generally, these languages are more complex with added features like [[operator overloading]], functions that masquerade as values (properties), and many other features intended to aid in building large programs. These sorts of features have more in common with C++
===Memory handling===
Line 61 ⟶ 65:
</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
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 function {{code|countTheNumberOfUsers}} that returns an integer, and an integer variable, {{code|theCountedUsers}} that holds the result. In many languages, a [[Magic number (programming)|magic number]] would be placed in {{code|theCountedUsers}} to indicate that {{code|countTheNumberOfUsers}} has not yet been called, while many implementations would just set it to zero. In Zig, this could be implemented as an {{code|var theCountedUsers: ?i32 {{=}} null|lang=zig}} which sets the variable to a clear "not been called" value.<ref name=zigo/>
Line 82 ⟶ 86:
const c = @cImport(@cInclude("soundio/soundio.h"));
</syntaxhighlight>
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
Zig treats cross-compiling as a first-class use-case of the language. 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]] 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/>▼
===Comptime===
Line 98 ⟶ 99:
This function takes in some type {{code|T}}, and returns a custom {{code|struct}} defining a linked list with that data type.
==Compiler==
Zig is not just a new language: it also includes a C/C++ compiler, and can be used with either or both languages.
The name 'Zig' was reportedly chosen through a process involving a [[Python (programming language)|Python]] script that randomly combined letters, starting with the letter 'Z' and followed by a vowel or 'Y', in order to generate four-letter words. Despite the intended length, 'Zig', a three-letter word, was ultimately selected from the various combinations produced by the script.<ref>{{cite web |url=https://gist.github.com/andrewrk/73742bf4b8ed795c85ce |title=origin of the zig programming language name. by @andrewrk |author=andrewrk |date=2024-03-13 |access-date=2024-03-13}}</ref>▼
The
▲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]] 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).
▲Zig is not just a new language: it also includes a C/C++ compiler, and can be used with either or both languages.<!-- "Zig competes with C instead of depending on it" -->
▲Zig has a few drawbacks. If memory is not properly deallocated, it can result in memory leaks due to the lack of hidden control.<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> The learning curve for Zig can be steep, especially for those unfamiliar with low-level programming concepts.<ref name="logrocket"/> Although Zig has a growing community, as of 2024, it remains a new language with areas for improvement in maturity, ecosystem and tooling.<ref name="logrocket"/> Interoperability with other languages may pose challenges, as it often requires extra effort to manage data marshaling and communication.<ref name="logrocket"/> Finally, the availability of learning resources is limited for complex use cases, though this is gradually improving as interest and adoption increase.<ref name="logrocket"/>
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 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.
==Packages==
▲The older [[Bootstrapping (compilers)|bootstrapping]] ("stage1") compiler is written in Zig and [[C++]], using [[LLVM]] 13 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> The compiler is [[free and open-source software]] released under an [[MIT License]].<ref>{{Cite web|url=https://github.com/ziglang/zig|title=ziglang/zig|website=GitHub|language=en|access-date=2020-02-11}}</ref> The Zig compiler exposes the ability to compile C and C++ similarly to [[Clang]] with the commands <code>zig cc</code> and <code>zig c++</code>,<ref>{{Cite web|url=https://ziglang.org/download/0.6.0/release-notes.html#zig-cc|title=0.6.0 Release Notes|website=Ziglang.org|access-date=2020-04-19}}</ref> providing many headers including the [[C standard library]] (libc) and [[C++ Standard Library]] (libcxx) for many different platforms, allowing Zig's <code>cc</code> and <code>c++</code> sub-commands to act as [[cross compiler]]s out of the box.<ref>{{Cite web|title='zig cc': a Powerful Drop-In Replacement for GCC/Clang - Andrew Kelley|url=https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html|access-date=2021-05-28|website=andrewkelley.me}}</ref><ref>{{Cite web|title=Zig Makes Go Cross Compilation Just Work|url=https://dev.to/kristoff/zig-makes-go-cross-compilation-just-work-29ho|access-date=2021-05-28|website=DEV Community|date=24 January 2021 |language=en}}</ref>
▲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.
▲Before version 0.11.0, Zig did not have a [[package manager]], but in 0.11.0 an experimental version was released which was further extended in version 0.12.0. There is no official [[package repository]]; instead a package is simply a URL pointing to a [[Compressed file library|compressed file]] that, when uncompressed, includes a standard build.zig file (that the Zig compiler uses by convention to compile the source code) and, ideally, a build.zig.zon file which is used to define the name and version of the package.
▲Zig 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>
==Examples==
Line 142 ⟶ 135:
return struct {
const Self = @This();
pub const Node = struct {
next: ?*Node = null,
Line 156 ⟶ 150:
list.first = new_node;
}
pub fn format(
list: Self,
Line 163 ⟶ 158:
) !void {
try out_stream.writeAll("( ");
var it = list.first;
while (it) |node| : (it = node.next) {
Line 172 ⟶ 168:
1,
);
try out_stream.writeAll(" ");
}
try out_stream.writeAll(")");
}
Line 181 ⟶ 179:
pub fn main() !void {
const ListU32 = LinkedList(u32);
var list = ListU32{};
var node1 = ListU32.Node{ .data = 1 };
var node2 = ListU32.Node{ .data = 2 };
var node3 = ListU32.Node{ .data = 3 };
list.prepend(&node1);
list.prepend(&node2);
list.prepend(&node3);
try stdout.print("{}\n", .{list});
try stdout.print("{b}\n", .{list});
Line 240 ⟶ 241:
);
try stdout.print("{s}\n", .{repeated});
}
Line 247:
* Output
*:<syntaxhighlight lang="output">
Hello Hello Hello
</syntaxhighlight>
==Name==
▲The name
== Community ==
Line 254 ⟶ 257:
== Projects ==
* [[Bun (software)|Bun]] is a [[JavaScript]] and [[TypeScript]] runtime written in Zig, using [[Safari (web browser)|Safari]]
* Ghostty<ref>{{Cite web |title=ghostty-org/ghostty |url= https://github.com/ghostty-org/ghostty |access-date=2024-12-27 |website=Github |language=en}}</ref> is a [[Terminal emulator|terminal emulator]] written in Zig.
* 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.
|