15 ms·
Why would anyone use a new systems programming language that is not safe? Zig might be better than C in some aspects, but is it worth it to switch to Zig when y
by beltsazar 4y ago
Why would anyone use a new systems programming language that is not safe? Zig might be better than C in some aspects, but is it worth it to switch to Zig when you realize that it's not much safer than C? [0]
[0]: https://www.scattered-thoughts.net/writing/how-safe-is-zig/ https://www.scattered-thoughts.net/writing/how-safe-is-zig/
- rtev 4y agoIt does seem confusing to me why someone would choose Zig. If you’re looking for a low-level language, Rust is safer with a better ecosystem. If you want a high-level language, Nim binaries are smaller and the Go ecosystem is better. When is Zig the best choice?
- ferdowsi 4y agoI would choose Zig because Zig has a much more approachable learning curve and Zig code is eminently legible compared to Rust code and it's overly complex type system.
- k__ 4y agoWhy not Go or Nim?
- stephc_int13 4y agoBecause Garbage Collection is basically a garbage feature that should be, at the very least, optional.
- kitkat_new 4y agoit's overly complex because ...?
- acuozzo 4y ago> When is Zig the best choice? "Freestanding" targets, perhaps? I often write software in C to run directly on microcontrollers; no OS, no runtime. I can see the advantage freestanding Rust would provide for e.g. 802.11 firmware. Broadcom repeatedly suffers from their choice to write the firmware for their WiFi chips in C. … but would Rust provide any clear advantage to the programmers of a Furby or a keyboard controller?
- Cyberdog 4y agoThe fanaticism of Rust devs makes me think it's probably massively overrated (see Node yesterday and Ruby/Rails the day before) and Go is associated with Google which gets a perhaps unfair but still unignorable knee-jerk reaction from me to avoid it. I don't know enough about Nim to pass judgment. Two reasons I decided to give Zig a try: The official chat channel is on IRC, instead of Discord or Slack (so the people involved care about efficiency, open standards, and avoiding trends/bandwagoning), and it has an early but promising-looking Swift UI-like cross-platform UI framework in development: https://github.com/capy-ui/capy https://github.com/capy-ui/capy
- davidatbu 4y agoI'm not at all trying to change your mind (I've never used Zig so I can't comment there), but just FYI: 1. Rust uses Zulip (ie, not Discord or Slack), 2. Rust doesn't lack for promising-looking, declarative/reactive UI frameworks: https://iced.rs/ https://iced.rs/, https://crates.io/crates/egui https://crates.io/crates/egui, for example.
- Arnavion 4y ago>1. Rust uses Zulip (ie, not Discord or Slack), The official Rust chat is on Discord. Now, to Cyberdog's point, there is an IRC channel on libera (that I'm also a part of) and it's even bigger than Zig's channel, but it's "unofficial" and they did ask for an official one. Like, Zig's channel has the advantage of being a direct line to andrewrk which you won't get from the Rust channel.
- davidatbu 4y agoThanks for the correction! I conflated what (I believe) some rust teams use as the thing that every thing in official rust uses. I think Rust uses both Discord and Zulip[0]. [0] https://zulip.com/case-studies/rust/ https://zulip.com/case-studies/rust/
- tialaramex 4y agoThere's no equivalent to a "direct line to andrewrk" because Rust does not have this one man setup. Culturally, there's more of a "pitch in to help" and less "ask before touching". So people are mostly looking for hand-holding, "How do I?" and "Is this correct?" rather than decisions and permission.
- zppln 4y agoSome might find Zig to have better ergonomics and some work on systems where dynamic memory allocation and concurrency are inappropriate for safety reasons anyway.
- xedrac 4y agoI could see Zig thriving in a kernel, with its seamless integration with C. It would be a relatively simple upgrade to make. I still think Rust is the better choice though. Maybe the answer to your question is, if you're strictly a C developer and you want something a little nicer without too much more, then Zig might be the best choice for you.
- deleted 4y ago[deleted]
- cztomsik 4y agofor me, - easy interfacing to C libs (glfw) - very easy and readable metaprogramming (compile time inline for over all enum variants, duck-typed and strong-typed anytype at the same time, @compileError for advanced compile checks so you can teach compiler new rules about your code instead of trying to fit your code in the extisting compiler rules)
- Tozen 4y agoVery good points, that can't be ignored. Zig looks to be trapped in a niche. It's going to have a really hard time pushing past other low-level languages, and because its small and focused on that area, will not be able to compete with numerous high-level general-purpose languages that do more for users. Add to that, Rust and Go have strong corporate backing, continually get lots of shine in the media, and often are given positive spin or ratings whenever compared to other languages. As you mentioned Nim, look how far uphill it still has to climb, after the many years (around 14 years) it has been out. TIOBE Index doesn't even have Nim in their top 50, and they aren't even on IEEE's top programming languages list (of around 60 languages). This is just as likely the fate of Zig as well. Not saying some people won't find Zig useful for particular niches, but rather it will likely never be on the level of Rust, Go, D, Object Pascal, etc... or possibly even Nim.
- azakai 4y ago"Not much safer" isn't fair. Zig has full spatial memory safety, which is already a huge improvement over C. The Zig type system and how idiomatic code looks also prevent various safety risks. Also, Zig panics on integer overflow, avoiding various other dangers. Zig does lack a general solution for temporal memory safety. That is a downside. But it can still have that safety in ReleaseSafe mode, at least - for example, by not reusing memory addresses. That adds overhead, which might rule Zig out in certain areas, but not others. Also, while in many domains safety should be the #1 factor when when choosing a language (like building a web browser), that's not universally true. Other factors exist.
- flohofwoe 4y agoTemporal (runtime) safety could actually be provided via tagged index handles (where the tag is a generation count). But this should be implemented by libraries, not built into the language (like: https://github.com/michal-z/zig-gamedev/tree/main/libs/zpool https://github.com/michal-z/zig-gamedev/tree/main/libs/zpool) (it's of course not a "general solution", but I wonder if something like 'tagged pointers' could be that, even without hardware support, and at some runtime cost)
- pcwalton 4y ago> Zig has full spatial memory safety, which is already a huge improvement over C. I don't believe this is true. Zig has pointers to unknown numbers of items, which don't seem bounds checked: https://ziglang.org/documentation/master/#Pointers https://ziglang.org/documentation/master/#Pointers They prefer slices idiomatically, but that's not "full spatial memory safety". C also prefers you to pass the length of every array whenever you pass a pointer to it, in that correct code must do this. But the entire point of language memory safety is that we don't trust programmers to consistently do the right thing. You might be able to say something like "Zig minus features X, Y, and Z has full spatial memory safety". I'd be interested to see what features those are: it looks like at a minimum you would have to get rid of multi-element pointers and extern unions. > But it can still have that safety in ReleaseSafe mode, at least - for example, by not reusing memory addresses. The overhead is extremely high, because it leaks an entire 4kB page if a single allocation from that page is still alive. In the worst case, it's equivalent to rounding every allocation up to 4kB. If you're OK with that overhead, you could just link in the Boehm GC and get the same safety with less memory usage, and as an added benefit you wouldn't have to call free anymore. > Also, while in many domains safety should be the #1 factor when when choosing a language (like building a web browser), that's not universally true. Other factors exist. In the vast majority of those domains, you could just use a GC'd language. I don't see much room for a new language that isn't memory-safe in 2022.
- acuozzo 4y agoI clicked the link and I see within the table mentions of "heap" and "free", but I regularly target systems with no runtime. Does Rust offer many advantages over Zig in this space? I mostly program in C, but I've been eyeballing both for some time now.
- flumpcakes 4y agoI think Zig would be much easier to write code for compared to Rust in this use case. Rust is not some magical tool that stops all bugs as some people like to think it is. The lower you go, the more you will have to contort yourself, and the more likely it is for less experienced programmers to make mistake. I think Zig is much more approachable and fixes a lot of C foot guns and guides you into writing safer, faster code. If you are writing web apps then Rust is probably "safer". If you are writing low level code that requires obtuse memory management then I don't think you can make a blanket claim that Rust is just "safer". A lot of this nuance is not often discussed.
- oconnor663 4y agoThe simple answer to your question (if you really meant it as a question) is that memory safety comes with ergonomic and performance costs. The Rust project is an experiment in hammering those costs down as small as possible, and sometimes maybe even turning those costs into benefits. But the costs do exist, and of course not every project in the whole world will want to pay them.
- cxr 4y agoRust cares about a subset of PL ergonomics, sure. But it is not true that Rust as a project is an experiment in hammering down costs on ergonomics as small as possible. If that were true, the Rust toolchain wouldn't be what it is.
- flohofwoe 4y agoThe world isn't just black and white, as is demonstrated by Rust's unsafe keyword (and Rust would be pretty much useless as systems programming language without unsafe).
- deleted 4y ago[deleted]
- k__ 4y agoYes, I never got the USP of Zig. There is Go, Nim, and Rust. All pretty modern system programming languages and some of them even close to C performance.
- ArrayBoundCheck 4y ago> systems programming language that is not safe? It sounds like you're saying rust because I don't know what other languages claim both. Rust is an awful language. That's why. If you're using a systems programming language then chances are unsafeness isn't the problem you're most concerned with
- Ar-Curunir 4y agoI too love to create kernels/browsers/any network-interacting system that have horrible memory safety issues.
- pron 4y agoI don't agree at all about "not much safer" and Zig differs from C in so many ways -- including but not limited to safety -- but regardless, memory safety comes at a price, language safety is not an end (that would be program correctness) but just one of many means to that end, there is no consensus at all over what is the most effective way to reach that end, and there are other things people care about as well. So your very question is the wrong one. You're both setting an absolute priority as well as the best way to achieve it when there's no consensus over either one of these. Put another way, the answer to your question is because even if program correctness were the only value that matters to anyone who does low level programming (and it isn't), there's no agreement sound memory safety in the language -- at least the way Rust does it -- is the best way to achieve correctness. Put yet another way, the answer is: for the same reason people would want to use a language with memory safety -- because they think it helps them achieve their goals better.
- stephc_int13 4y agoAs far as I am concerned, the alleged "memory safety" benefits of Rust do not have much more value or weight than wishful thinking, this kind of claim can only gain strength by standing the test of time. Computer security is a real problem that must be fully addressed at every step of the design of a system, language and tooling can help, a bit, not much. Rust is not a "safe" language. It might be -safer- than C/C++, but only time will tell if the difference is significant.
- hsn915 4y agoHere's a hypothesis I'd like to see Rust advocate attack: In any serious system written in Rust, significant portions of it will have to be written in "Unsafe Rust".
- dtolnay 4y agohttps://github.com/diem/diem https://github.com/diem/diem is 387,952 lines of Rust code across 169 crates. In total there are 3 lines of unsafe code, which is less then 0.001% (1 thousandth of 1%).
- deleted 4y ago[deleted]
- hitekker 4y ago> The launch was originally planned to be in 2020 but only rudimentary experimental code has been released until the project was abandoned in January 2022. https://en.wikipedia.org/wiki/Diem_(digital_currency) https://en.wikipedia.org/wiki/Diem_(digital_currency)
- steveklabnik 4y agoYou’d have to demonstrate that this is the case before it’d be worthy of “attacking.” You’d also have to define “serious system” and “significant portion” and if you really do mean “any,” that is, is a single counter example enough, and if not, what percentage of them would need to be so for this to be true. You’d also have to say what’s in bound or not, is it just the code you wrote for the system, is it any dependencies, etc etc etc. Does “significant” mean “large amount” or does it mean “most important”?
- emmanueloga_ 4y agoWhy Zig When There is Already C++, D, and Rust? [1]. Also, safety is not the only consideration when writing software right? Otherwise, all software would have to be written using formal methods [2] ... clearly overkill for many (most?) applications. Maybe it is a bit obvious, but I think someone would pick Zig because they like the set of features that the language provides, allowing this person to write reasonably bug free code in (hopefully) shorter time than using a different language or methodology. I think most ppl choose the language because they like the tools/ecosystem/libs available, because it aligns with previous experience/s, because of how popular it is for the kind of app they are writing, and because they agree with the authors philosophy (as opposed to choosing it based on any particular language features). -- 1: https://ziglang.org/learn/why_zig_rust_d_cpp/ https://ziglang.org/learn/why_zig_rust_d_cpp/ 2: https://en.wikipedia.org/wiki/Formal_methods https://en.wikipedia.org/wiki/Formal_methods
- LAC-Tech 4y agoIt's safer than C and easier to use than rust.
- williamstein 4y agozig is not meant to be just a systems programming language. It's stated goal is "Zig is a general-purpose programming language and toolchain for maintaining robust, optimal and reusable software." and Andrew really does intend for it to be useful far beyond systems programming. I hope they succeed, since I've been using zig regularly for over a year now, and I love it. I have decades of experience with assembly, Fortran, C, C++, Python, and Javascript, and I really appreciate what the Zig developers are trying to do. I hope they succeed!