6 ms·
Learning Zig
- ukd1 3y agoNice post. I learnt via https://ziglings.org https://ziglings.org - koans where you fix minor, and increasingly hard things to learn a languages concepts.
- satvikpendem 3y agoRust also has something similar which is where I believe Zig drew inspiration from as well: https://github.com/rust-lang/rustlings https://github.com/rust-lang/rustlings
- amj7e 3y agoOk... I guess
- satvikpendem 3y agoWhat are the pros and cons of Zig vs Rust? I see Zig mentioned more and more here, especially with regards to threads about Bun, the largest / most popular project in Zig currently, but I'm not sure why I should use it over Rust. Zig seems like it still does not solve the problem of memory safety, in that it has safety issues that would be caught by a borrow checker or other such memory safety checker [0]. You can still use after free, for example: var hello = try allocator.dupe(u8, "hello world"); allocator.free(hello); std.debug.print("{s}\n", .{hello}); [0] https://www.scattered-thoughts.net/writing/how-safe-is-zig/ https://www.scattered-thoughts.net/writing/how-safe-is-zig/
- eggy 3y agoFor me, Zig is more of a better C plus. If I want memory safety and high-integrity software at this point, I use SPARK2014, a subset of Ada. It has a longer legacy for mission-critical software than Rust. Adacore, a private company that provides support for Ada and SPARK, are collaborating with Ferrous Systems to bring all the goodness to Rust, but Rust does not have the legacy applications or all the goodies (yet).
- boberoni 3y ago“Why Zig When There is Already C++, D, and Rust?” https://ziglang.org/learn/why_zig_rust_d_cpp/ https://ziglang.org/learn/why_zig_rust_d_cpp/
- scythe 3y agoZig is trying to be a new C. Rust is trying to be a low-level ML. Sometimes people say that "Rust is trying to be the new C++" but this is only true in a more abstract sense that looks at use cases — it doesn't follow the same technical path C++ did. You could probably make a reasonable analogy that Zig:Rust::Go:OCaml but this isn't perfect. Zig is simpler to work with but it doesn't have as many powerful features, like certain Rust safety guarantees, as you mentioned.
- wredue 3y ago>doesn’t have as many powerful features comptime is easily one of the most powerful language features of any language in existence. Comptime gives you literally everything you get from macros and generics without the complexity of needing to know 3 separate Turing complete languages to get it. Zig doesn’t need a horde of random highly complex differently rule bounded features all complexily interacting with one another because it has 1 easy to use feature that eliminates those needs.
- satvikpendem 3y agoAh, if only Rust didn't drive away the person who was working on the comptime equivalent in Rust. Sadly, now it is likely that that will be a long way off.
- Ygg2 3y agoIf only 1% people whining about comptimes actually sat down and looked into adding it, we'd have comptimes already. Disclaimer: I'm tentatively exploring this via macros.
- veber-alex 3y ago> Comptime gives you literally everything you get from macros and generics without the complexity of needing to know 3 separate Turing complete languages to get it. comptime doesn't allow you to create domain specific macros like html! in Rust, it also doesn't allow you to constrain what types are accepted by your functions. It also has its own problems with long compile times.
- dijit 3y agoRust is “a better C++” (for lack of a cleaner allegory). Zig is “a better C”. Understanding that makes it clear where Zig shines and Rust is less ergonomic. Both are good, but I see them as complementary and not really adversarial or in competition.
- flohofwoe 3y agoIn terms of safety, Zig is somewhere in the middle between C and C++ on one side, and Rust on the other side (e.g. it enforces much more correctness than C or C++, and has runtime spatial, but not temporal memory safety (but it has a debug allocator which doesn't recycle memory (or rather: address ranges) and which should catch most temporal memory safety problems - like your example). Zig doesn't have separate sublanguages for macros and generics, instead these are all handled with regular 'comptime' Zig (along with type reflection). Zig has builtin syntax sugar for dealing with optionals and error unions, which makes code less noisy (although that may be subjective - and I think Rust also got some of that over time). Zig's interaction with the C world is excellent, and to a lesser degree also to the C++ and ObjC world (the Zig compiler can compile C++ and ObjC code, but still requires a C shim to sit between Zig and C++ or ObjC code). Zig has much less influence from high level functional languages and less "type system wankery" compared to Rust, this again is a good thing for some people, while disliked by others. A minor detail, but which I think demonstrates the Zig philosophy: the Zig compiler executable is also the build system and package manager. In Rust this is delegated to a separate tool (cargo). ...and my personal opinion: Zig is a lot more 'elegant' than Rust which results in more joy when writing Zig code. Rust feels entirely too much like 'designed by committee'. With Zig I "grokked" the language in a weekend of tinkering, while with Rust I never really had "fun" despite several attempts of trying to like it. TL;DR: If I had to create an absolutely waterproof and critical isolated piece of code, I would probably write that in a "simple Rust subset". For anything else I would definitely prefer Zig.
- throwawaymaths 3y agoIs memory safety the only thing holding you back? Suppose there were a way to do static borrow checking, as a 3rd party tool?
- naruhodo 3y agoThe C/C++ interop in Zig gives it a great value proposition for embedded systems programming. My experience of that culture has been that C++ is "too unpredictable", so C is the language of choice. And glob help you if you want to use dynamic memory allocation. Zig could replace C and the preprocessor and support programming at a higher level of abstraction (generics). I've tried a couple of times now to get my head around Rust with small efforts. Apparently I need big efforts and deep immersion. "A half-hour to learn Rust"[1] certainly helped with understanding the syntax around lifetimes. I keep reading that understanding the borrow-checker and lifetimes should be a snap for C/C++ programmers. Well, I've been doing C++ for over 30 years and C for longer, and Rust just strikes me as a bondage and discipline language. [1] https://fasterthanli.me/articles/a-half-hour-to-learn-rust https://fasterthanli.me/articles/a-half-hour-to-learn-rust
- grayhatter 3y agoYou shouldn't use it over rust... that's a nonsensical question. That's like asking when you should use a hammer instead of a screwdriver. I use zig because it's enjoyable to use. I hate writing rust, it feels like the language is actively fighting me. I started writing zig, and started loving the process of simplify writing code again. Zig is nice because, to steal their quote. I'm able to spend my time debugging my code, and not debugging my understanding of the language. I've stepped on a use after free but because it's so easy to write and use tests, I found it and fixed it in tests. Bugs are undesirable, but I can write just as many bugs in rust. So I'm always so confused about why rust users are so panicked about use after free, or other memory bugs. Like somehow the only bugs rust has a handle on is the worst class of bug? Such an odd idea I can't figure out...
- satvikpendem 3y agoBecause I can either catch those bugs myself or let the computer catch them for me. I'm lazy and a fan of letting the computers do the work at compile-time so I don't have to run into them at runtime, at 3 AM in production. It's not the amount of bugs, I can also write out of bounds panic bugs, it's the type of bugs. And truth be told, if we get liquid types or dependent types in Rust, even those runtime out of bounds checks would be caught at compile-time too.
- grayhatter 3y agoThis argument doesn't make sense to me either, but maybe I'm just stupid. For something to die at 3am in production, that means either someone pushed code way too late, or something changed in the stack unpredictability. Your compiler wouldn't have been able to predict either of those cases. But bugs are bugs, if I'm called at 3am to fix a bug, I don't want to spend hours trying to fight my compiler, and then fall back asleep while I'm waiting for it to compile. I want to quickly identify the issue, and then to be able to write the smallest possible patch until morning. In my limited experience, rust is conducive to neither of those options. And you said "if we get liquid types" I assume that means you meant it to say if rust gets? Unless you meant to identify as part of the rust language?
- mamcx 3y agoRust is what most *should* use for system-level programming and/or overall programming, if possible. Rust is safer, better overall + ergonomics, ecosystem, etc. However, Zig have some nice properties: - Is easier to do bit/memory wrangling in Zig than in Rust - Is FAR easier to cross-compile. To the point that for example I use Zig to compile my Rust project for musl - Is MUCH faster to compile - Is far "smaller" that is nice in constrained situation (not just "environments", for example, you can use zig for when you will use gcc/llvm to compile some small c file) Zig is a very good match if the scope of the project is relatively small. Rust shine much more when is more big or you need to work for a team of people. Also, if you plan to make a library that will be used by others, better to use a safer lang. I don't see both as competitors. They are excellent complements, and my dream is to Zig to replace C and Rust/ada/pascal to replace C/C++. P.D: A probably good match is to make some low-level thing in Zig (where before was C) and FFI it in Rust.
- matheusmoreira 3y agoZig is not supposed to be a Rust equivalent. It's supposed to be C but with the problems fixed. https://youtu.be/Gv2I7qTux7g https://youtu.be/Gv2I7qTux7g
- Dwedit 3y agoZig doesn't have any features integrated into the language to make reference-counted objects painless to use. For that matter, neither does C#.
- throwaway17_17 3y agoI would assume this stems from Zig’s commitment to ‘No Hidden Control Flow’. By nature, syntactic sugar for reference counting will involve the calling of functions inside the code the sugar is standing in for. This would then require adding the RC constructs as language primitives and hard code their implementation into the language. As such, I don’t think adding RC and a ‘painless’ syntax to use it is likely to be a feature Zig picks up or settles on for memory management in the language. This seems to be a hard line for Andrew, I don’t think he is unable to consider adding primitives to Zig, but the sense I get from his recorded presentations is that the primitive collection in Zig currently is very unlikely to expand as Zig moves to 1.0. NOTE — I am not affiliated with Zig and don’t know how the process of language design works for them outside of A. Kelly’s being assumed to be BDFL and having final say.
- BaculumMeumEst 3y agoDoes anyone know why Zig isn't up to date on homebrew (v10.1, while v11 is latest)? I tried pulling a very simple SDL2 project off github and building it on my macbook M1 and hit https://github.com/ziglang/zig/issues/12069 https://github.com/ziglang/zig/issues/12069. Homebrew also has no option for --HEAD, so I don't know if I'm running into a fixed issue or what. If anyone has recommendations or an example scaffold that shows how I can build a SDL2 zig project on a M1 with SDL2 installed via homebrew that would be helpful. I can't get Rust-SDL2 to work on my system either, so maybe I'm just forever cursed to use C++. At least I understand how to interface with system libraries without playing build system Jenga I guess. Edit: I manually installed a later version. I tried a few small SDL2 projects, but trying to run them all gave me: ```error: no field or member function named 'standardReleaseOptions' in 'Build' const mode = b.standardReleaseOptions();``` Perhaps the build system had some breaking changes at some point. I would need to figure out how the old build system worked, as well as what changed, in order get past it. OTOH ChatGPT was able to get me past my Rust issue (I gave it the output of sdl2_config and it spat out some environment variables to add to my ~/.profile which fixed my issue), so I will come back to Zig when it's a little more stable
- kristoff_it 3y agoJust download Zig from https://ziglang.org/download/ https://ziglang.org/download/. Manually editing your path seems a better alternative than being cursed to forever use C++.
- flohofwoe 3y agoBtw, an official 'zigup' tool which can install multiple Zig version side by side and switch between them would be a real life saver, especially for switching between nightly and stable. Having to find the right download link, unpacking and setting up paths is a bit too 90's ;) (especially when trying to keep up with nightlies). This would also a great tool for easily setting up a Zig toolchain on CI servers.
- michaelsbradley 3y ago