Zig (programming language): Difference between revisions

Content deleted Content added
WikiCleanerBot (talk | contribs)
m v2.05b - Bot T20 CW#61 - Fix errors for CW project (Reference before punctuation - Link equal to linktext)
m remove arbitrary fixed sizes
Line 3:
| name = Zig
| logo = Zig logo 2020.svg
| logo_size = 180px
| paradigms = [[Multi-paradigm]]: [[Imperative programming|imperative]], [[Concurrent computing|concurrent]], [[Procedural programming|procedural]], [[Functional programming|functional]]
| designer = Andrew Kelley
Line 32 ⟶ 31:
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> (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}}), 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> 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> Like C, Zig omits [[Garbage collection (computer science)|garbage collection]], and has [[manual memory management]].<ref name=zig /> To help eliminate the potential errors that arise in such systems, it includes [[option type]]s, a simple [[Syntax (programming languages)|syntax]] for using them, and a [[unit testing]] framework built into the language. Zig has many features for [[Low-level programming language|low-level programming]], notably packed structs (structs without padding between fields), arbitrary-width integers<ref>{{Cite web |url=https://www.theregister.co.uk/2020/04/24/llvm_project_adds_support_for/ |title=Keen to go _ExtInt? LLVM Clang compiler adds support for custom width integers |last1=Anderson |first1=Tim |date=2020-04-24 |website=www.theregister.co.uk |language=en |access-date=2024-12-30}}</ref> and multiple pointer types.<ref name="zigdocumentation">{{Cite web|url=https://ziglang.org/documentation/master/|title=Documentation|website=Ziglang.org|access-date=2020-04-24}}</ref>
 
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, 2024 |website=The Next Web |accessdate=Dec 15, 2024}}</ref> Despite this, a [[Stack Overflow]] survey in 2024 found that Zig software developers earn salaries of $103,000 USD per year on average, making it one of the best-paying programming languages.<ref name="VB1">{{cite web |url = https://venturebeat.com/programming-development/3-programming-languages-you-need-to-know-about/ |title = 3 programming languages you need to know about |last = McBride |first = Aoibhinn |date = July 16, 2024 |website=[[VentureBeat]] |accessdate = Dec 15, 2024}}</ref> However, only 0.83% reported they were proficient in Zig.<ref name="TNW1" />
Line 47 ⟶ 46:
One of the primary sources of [[Software bug|bugs]] in C programs is the [[memory management]] system, based on [[malloc]]. malloc sets aside a block of memory for use in the code and returns a reference to that memory as a [[pointer (computer science)|pointer]]. 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, a [[memory leak]]. More common is a [[dangling pointer]] that does not refer to a properly allocated memory object.<ref name=gc/>
 
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{{factcitation 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, {{asofas 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 89 ⟶ 88:
By using the {{code|comptime}} keyword, the programmer can explicitly have Zig evaluate sections of code at [[compile time]], as opposed to [[Runtime (program lifecycle phase)|runtime]]. Being able to run code at compile time allows Zig to have the functionality of [[Macro (computer science)|macros]] and [[conditional compilation]] without the need for a separate [[preprocessor]] language.<ref name="roadtozig1.0">{{cite AV media |url=https://www.youtube.com/watch?v=Gv2I7qTux7g |title=The Road to Zig 1.0 - Andrew Kelley |publisher=ChariotSolutions |via=[[YouTube]] |date=2019-05-09}}</ref>
 
During compile time, types become [[Firstfirst-class citizen|first-class citizens]]s. This enables compile-time [[duck typing]], and is how Zig implements generic types.<ref name="zigdocumentation"/>
 
For instance, in Zig, a generic [[linked list]] type might be implemented using a function like: