7 ms·
What are the use cases for zig? The website says general purpose and tool chain but people who have used it, what it excels at?
by tarcio 3y ago
What are the use cases for zig? The website says general purpose and tool chain but people who have used it, what it excels at?
- johnnypangs 3y agoBun, a JavaScript runtime (uses WebKit as an engine I believe) is written in Zig. They seem to be doing alright: https://bun.sh/ https://bun.sh/
- BigJ1211 3y agoIt uses JavaScriptCore, which was written for Safari. It's supposedly faster than V8, but harder to work with.
- skavi 3y agoA fancy new database is written in Zig: https://tigerbeetle.com/ https://tigerbeetle.com/
- pjmlp 3y agoBeing Modula-2 like (in safety), with a syntax that is more appealing to C minded folks, with nice compile time execution support.
- ksec 3y agoI think this is one of the best short description of Zig.
- tauoverpi 3y agoA game engine https://machengine.org https://machengine.org is being written in zig, there's also https://microzig.tech https://microzig.tech as zig is well suited to embedded development.
- armchairhacker 3y agoMy understanding is that Zig has all of the power and modernity of Rust, without the strictness and borrow checker. Unlike Rust, it also has powerful compile-time evaluation and custom allocators, and probably more improvements I'm not familiar with (in Rust you can effectively emulate custom allocators, but you have to rewrite every allocating structure to use them; or you can use nightly, but most third-party library and even some standard-library types don't support them). I also heard someone say "Zig is to C what Rust is to C++". Which I interpret as, it's another maximum-performance modern language, but smaller than Rust; "smaller" meaning that it has less safety and also abstraction (no encapsulation [1]), but less requirements and complexity. Particularly with games, many devs want to build a working prototype really fast and then iterate fast. They don't want to deal with the borrow checker especially if their code has a lot of complex lifetime rules (and the borrow checker is a real issue; it caused evanw to switch esbuild to Go [1]). In small scripts with niche uses, safety and architecture are a waste of effort, the script just has to be done and work (and the latter is only partly necessary, because the script may not even be fed enough inputs to cover edge cases). Plus, there are plenty of projects where custom allocation is especially important, and having every type support custom allocation is a big help vs. having to rewrite every type yourself or use `no_std` variants. [1] https://github.com/ziglang/zig/issues/2974 https://github.com/ziglang/zig/issues/2974 [2] https://github.com/evanw/esbuild/issues/189#issuecomment-647024185 https://github.com/evanw/esbuild/issues/189#issuecomment-647...
- lenkite 3y agoAlso none of the common knowledge around traditional data-structures and algorithms work with Rust anyways. One needs to dance like a ballerina with their hands and feet tied.
- pkulak 3y ago“Linked lists are hard” is not “none of the common knowledge around traditional data-structures and algorithms work”.
- DeathArrow 3y ago
- Cloudef 3y agoAs someone who used C as main language, I've switched to zig. It's the only language that tries to be "better C", and not another C++. Comptime being like Nim where it's not entirely own language is also plus. I'd say it excels at general purpose system programming, and especially if you need to work with memory in detailed way (rust makes this very annoying and hard).
- DeathArrow 3y agoWhat advantages does Zig have over C?
- lionkor 3y ago- comptime, so writing compile time evaluating code, without introducing its own meta language like macros or templates. - very solid build system (the build config file(s) are written in zig so you dont have to learn another language for the build system (looking at you, makefile)) that has crosscompilation builtin (with one compiler flag) - language level errors, like, errors as first class citizens. forces you to handle errors, but without much mental or syntactic overhead (you can re-throw them with `try myfunction()`), also results in a unified interface - no implicit conversions - looks high-level (modern sytnax that is easy(ish) to parse) but as low level (or lower) than C, no abstractions that hide details you need to know about when programming (similar to C) - C interop, so you can just add zig source files to a C project and compile it all with the zig toolchain. Zig can also parse c headers and source files and convert them, so you can include c headers and just start calling functions from there. For example, using SDL is as simple as pointing the toolchain to the SDL headers and .so file, and the sdl headers will be translated to zig on the fly so you can start with SDL.SDL_CreateWindow right away.
- deleted 3y ago[deleted]
- kristoff_it 3y agohttps://ziglang.org/learn/overview/ https://ziglang.org/learn/overview/
- bsder 3y agoZig has a decent chance of being an actual embedded device (ie. no operating system) programming language. In my opinion, Zig seems likely to grow the necessary bits to be good at embedded while Rust is unlikely to figure out how to shrug off the clanky bits that make it a poor fit for embedded devices. However, I'm a personal believer that the future is polyglot. We're just at the beginning of shrugging off the C ABI that has been preventing useful interoperability for decades. Once that happens, I can use Rust for the parts that Rust is good for and Zig for the parts that Zig is good for.
- tialaramex 3y ago> clanky bits that make it a poor fit for embedded devices. What do you see as "clanky bits that make it a poor fit" for such a broad range of stuff as "embedded devices" ? Embedded goes at least as far as from "It's actually just a Linux box" which is obviously well suited to Rust, to devices too small to justify any "high level language" where even C doesn't really fit comfortably. Rust specifically has no ambitions for hardware too small to need 16-bit addresses, but much of that range seems very do-able to me.
- bsder 3y agoExample clanky bit: memory ownership rules. There are lots of systems where memory changes hands. Game programming, for example. You allocate a big chunk of memory, you put some stuff in it, you hand it off to the graphics card, and some amount of time later the graphics card hands it back to you. Rust gets VERY cranky about this. You wind up writing a lot of unsafe code that is very difficult to make jibe with the expectations of the Rust compiler. It's, in fact, MUCH harder than writing straight C. Example clanky bit: slab allocation You often don't really care about deallocation of objects in slabs in video games because everything gets wiped on every frame. You'd rather just keep pushing forward since you know the whole block gets wiped 16 milliseconds from now. Avoiding drop semantics takes (generaly unsafe) code and work.
- tialaramex 3y agoI see, so by "embedded systems" you meant a video game console ?
- cztomsik 3y ago1. It's typically at least as fast as C, unlike C++/Rust 2. You can do type introspection (and switching) during compile-time, and it's not just some stupid TokenStream transformer, you really have type information available, you can do if/else on the presence of methods, etc. 3. There are no generics, but your functions can accept anytype, which is still type-safe. See https://github.com/ziglang/zig/blob/9c05810be60756e07bd7fee09633671bbaa8cda6/lib/std/math.zig#L263 https://github.com/ziglang/zig/blob/9c05810be60756e07bd7fee0... and note the return type is "computed" from the type of the input. 4. Types are first-class values (during comptime), so any function can take or return a new type, this is how you get generic types, without (syntax/checking) support for generics. 5. You can easily call anything which does not allocate in these comptime blocks. 6. There's @compileError which you can use for custom type assertions -> therefore, you have programmable type-system. 7. It's super-easy to call C from Zig. 8. This is subjective: You don't feel bad about using pointers. Linked data structures are fine.
- pjmlp 3y ago> 1. It's typically at least as fast as C, unlike C++/Rust The typical urban myth that never comes with profiler proofs.
- cztomsik 3y agoI don't need to prove you anything. Go and give it a try yourself.
- suby 3y agohttps://programming-language-benchmarks.vercel.app/c-vs-cpp https://programming-language-benchmarks.vercel.app/c-vs-cpp https://programming-language-benchmarks.vercel.app/c-vs-rust https://programming-language-benchmarks.vercel.app/c-vs-rust I understand that anytime someone brings benchmarks out, the next response points out that benchmarks are not real world use cases. Nonetheless, they are data points, and your claims are against the commonly accepted view of C being roughly as fast as C++ and Rust. If you have absolutely no data to back it up, you shouldn't expect anyone to believe you.
- mattdesl 3y agoFast and compact WASM builds are builtin to Zig's toolchain: https://github.com/mattdesl/wasm-bench https://github.com/mattdesl/wasm-bench
- jedisct1 3y agoFor WebAssembly, Zig really excels. It's very memory efficient, everything compiles of the box to WebAssembly (freestanding or wasi), the resulting code is compact and fast, and it can take advantage of the latest WebAssembly extensions (threads, SIMD, ...) without any code changes. If you are using a webassembly-powered cloud service and not using Zig to write your functions, you are wasting money. Seriously. Unsurprisingly, this is the most popular language to write games running on WebAssembly: https://wasm4.org/blog/jam-2-results/ https://wasm4.org/blog/jam-2-results/ Beyond the language, Zig is also a toolchain to compile C code to WebAssembly. When targeting WASI, it will seamlessly optimize the C library for size, performance or runtime features. I used it to port OpenSSL, BoringSSL and ffmpeg to WebAssembly. Works well. Also, Zig can generate WebAssembly libraries that can then be included in other languages that support WebAssembly. Most of my Rust crates for WebAssembly are now actually written in Zig. It's also supported by Extism, so can be used to easily write safe plugins for applications written in other languages.
- tempZigInterest 3y agoIf you don't mind, since you have experience targetting WASM with both Rust & Zig, what advantages does Zig have over Rust in this particular use case? Are the memory safety guarantees that Rust offers over Zig not as important or critical when targeting WASM? I've been interested in checking out Zig for a while now.
- djur 3y agoYour link is really interesting, but it shows that Zig was the most popular language for writing games for a WebAssembly-based fantasy console with constrained resources, not that it's the most popular language for WebAssembly-based games overall.
- SpaghettiCthulu 3y agoI've been thoroughly enjoying implementing a compiler and bytecode interpreter in Zig for a little scripting language I've been designing.
- binary132 3y ago“Whatever you would use C for but better” roughly I’m not really convinced because in Andrew’s livestreams he’s actively uncovered significant stdlib bugs that he is aware of and tables for later. Hopefully those will all be gone by 1.0, but I doubt it. For now, I cannot consider it a viable alternative to anything for production software. I do hope it will be some day, because it’s a nice language, even if it has a few syntactic warts :) that said... I feel safer choosing C89 or C99 for certain things due to its extremely wide availability and longevity. It’s great for it to have competitors, but C and C++ are more like standards and less like one tool with a handful of people working on it.