this post was submitted on 03 Oct 2024
40 points (87.0% liked)

Programming

17661 readers
58 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
 

Zig vs Rust. Which one is going to be future?

I think about pros and cons and what to choose for the second (modern) language in addition to C.

@[email protected]

top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 8 points 2 months ago

Since you're asking today the answer is Rust because it is already more mature. In 5-10 years if you asked them the answer might be different if zig sticks around.

This is no shade against zig! It's just very new. It doesn't have a 1.0 release yet.

Also, they're very different languages with very different goals. They aren't necessarily competing in the same space.

[–] [email protected] 17 points 2 months ago* (last edited 2 months ago)

Zig is a modern C. Rust is a (modern) alternative to C++. So two different languages can exist alongside each other, just like C and C++.

[–] [email protected] 1 points 2 months ago (1 children)
[–] [email protected] 4 points 2 months ago

Duzt.

There, i coined it.

[–] [email protected] 3 points 2 months ago

Why not both?

[–] [email protected] 3 points 2 months ago (1 children)

I hope that someone in the 40 comments i don't have time to read right now has pointed out that the premise of OP is flawed for the simple reason that Rust hit v1.0 in 2015, while Zig is still nowhere near that milestone in 2024.

So we are not even talking about the same "future" period from the start.

So, no need to get to the second false premise in OP, which is limiting a "future" period to one successful dominating language only. Nor is there a need to go beyond these premises and discuss actual language details/features.

[–] [email protected] 2 points 2 months ago (1 children)

I also hope that some of the people reading this realize that OP is also the person posting all of the "stop trying to suppress C" posts.

[–] [email protected] 2 points 2 months ago (1 children)

🤣

I don't know, and I don't want to get personal. But that's usually a sign of someone who doesn't even code (at non-trivial levels at least)*, and thinks programming languages are like sport clubs, developers are like players contracted to play for one and only one club, and every person in the internet gantry need to, for some reason, pick one club (and one club only) to be a fanboy of. Some people even center their whole personality around such fanboyism, and maybe even venture into fanaticism.

So each X vs Y language discussion in the mind of such a person is a pre-game or a pre-season discussion, where the game or season is an imaginary competition such people fully concoct in their minds, a competition that X or Y will eventually and decidedly "win".

* Maybe that was an exaggeration on my part. Some junior developers probably fall into these traps too, although one might expect, or maybe hope, that their view wouldn't be that detached from reality.


I'm hoping to finally finish and send out a delayed new release for one of my older and mature CLI tools this weekend. It's written in C btw 😄

[–] [email protected] 1 points 2 months ago

Modev says they've been using C for 25 years, and used Rust for several years as well! Their whole schtick baffles me.

[–] [email protected] 23 points 2 months ago* (last edited 2 months ago)

Google has already started to use Rust in their android operating system. Linux started getting Rust stuff. Rust has the speed of C/C++ while having memory safety. Zig does not have the same memory safety as Rust, it's a mere C/C++ alternative. Does that answer your question?

[–] [email protected] -2 points 2 months ago (3 children)

I'd say if you're happy with #C, there's no need to choose any second language. 🤷

Before even looking at any alternatives, the question should be "why not C". Some of the typical complaints are:

  • memory safety – or, more generally, the fact that C is only partially defined, leaving a lot to the dangerous area of undefined behavior. There's no way to reliably detect undefined behavior by static code analysis, and it will often hide well at runtime. Especially errors regarding memory management often directly expose security vulnerabilities. In my experience, the risk can be reduced a lot by some good coding and design practices. Examples are avoiding magic numbers, using sizeof everywhere possible, preferably on expressions instead of type names, defining clear ownership of allocated objects (and, where not possible, add manual reference counting), making ownership explicit by using const where appropriate and naming your functions well, and so on. Given also there's no guarantee alternative languages will safeguard you from all the possible issues and there are also a lot of other ways to create security vulnerabilities, my take on this would be: partially valid.
  • programming paradigm – you can only program in classic procedural style. Well, that's simply not true. First of all, you can program whatever you want in any turing-complete language, so what we talk about shouldn't be what's directly supported by the constructs of the language, but what's practically usable. In C, using some simple OOP is commonplace. Well, you can apply OOP to assembler programming without too much hassle, so, it's not surprising you can do it in C, as it's a pretty thin abstraction on top of machine code. C helps further with its linkage rules, where everything in a compilation unit is by default inaccessible to other units, and its incomplete types, where only a type name is known, but neither its size nor inner structure, giving you almost perfect information hiding for free. If you want/need polymorphism, it gets a bit more complicated (you have to think about how you identify types and manage virtual tables for them), but still perfectly doable. You'll hit the practical limits of the language when you want to go functional. The very basics are possible through function pointers, but most concepts of functional programming can't be applied to C in a somewhat sane way. Unless that's what you need, my take would be: invalid.
  • limited standard lib. Indeed, the C standard library misses lots of things that are often needed, for example generic containers like lists, hashtables, queues, etc. It's straight-forward to build them yourself, but then, there are many ways to do that (with pros and cons). You'll find tons of different implementations, many non-trivial libraries bring their own, of course this also increases the risk to run into buggy ones ... I wouldn't consider it a showstopper, but I'd mark this complaint as: valid.

Then, the next question would be: For what purpose are you looking for a second language?

For applications and services, there's already a wide range of languages used, and I'd say do what everyone does and pick what you're most comfortable with and/or think best suits the job. IOW, it makes little sense to ask what would be "the future", there have always been many different languages used, just pick one that's unlikely to quickly disappear again, so you'd have to restart from scratch.

For systems programming "the future" has been C for many decades 😏 ... some people want to change that, actually for good reasons. IMHO, the current push for #Rust (I don't see anything similar regarding #Zig yet?) into established operating systems is dangerous. An operating system is very different from individual apps and services. It's required by these as a reliable and stable (in terms of no breaking changes) platform. It's complex and evolves over an extremely long period of time. It needs to interface with all sorts of hardware using all sorts of machine mechanisms (PIO, DMA, MMIO, etc pp). I conclude it needs a very stable, proven and durable programming language. You'll find C code from the 30 years ago that just still builds and works. IMHO a key factor is that there's an international standard for the C language, governed by a standards body that isn't controlled by any individual group. And correlated to that, there are many different implementations of the language. Before considering any different language in core areas of an established operating system, I'd like to see similar for that new language.

Now, C was developed more or less together with #Unix, so back then, nobody knew it would be such a success, of course there was no standard yet. But then, I think this is the sane way to introduce a new systems programming language: Use it in a new (research?) OS.

[–] [email protected] 1 points 2 months ago

Thank you for such extended answer, I appreciate it.
Thank you for your experience!

[–] [email protected] 4 points 2 months ago (1 children)
[–] [email protected] 1 points 2 months ago

Nice, that looks like the way to do it!

"Funny" how it even offers a libc for compatibility ... but I guess this helps getting it some more serious testing 😉

[–] [email protected] 7 points 2 months ago (1 children)

For me you really aren't selling it.

When the answer to major draw backs with a language is use it better that's a dead end for me.

Some of the greatest programming minds have been using c for a long time and we still have a huge amount of dangerous vulnerabilities all the time.

The language is fundamentally flawed and other languages have demonstrated that you can get the same flexibility, expressiveness and performance without these flaws.

Again with the lack of many standard lib constructs. I now have to trust that every lib i use was written by a serious expert. as they'll need to implement so much themselves rather than trusting the core language team, who you hope would know it better than most.

And again with OOP. Why hack it into a language rather than use a language that supports it.

It's beginning to feel like people are just clinging to c because it's what they are used to. All I seem see are justifications of its flaws and not any reasons to actually use it.

If it came out today you'd have an incredibly hard time convincing anyone to use it over other languages.

[–] [email protected] 0 points 2 months ago (1 children)

When the answer to major draw backs with a language is use it better that’s a dead end for me.

Try browsing the list of somewhat recent #CVE rated critical, as I just did to verify. A majority of them is not related to any memory errors. Will you tell all them "just use a different programming language"?

And again with OOP. Why hack it into a language rather than use a language that supports it.

Have you seen existing C code? For anything non-trivial, most code uses some OOP, and it comes quite natural in C, certainly no "hacking". You don't need a class keyword to do that.

If it came out today you’d have an incredibly hard time convincing anyone to use it over other languages.

It doesn't come out today, it's been there for a long time, and it's standardized, proven and stable. Sounds like you seriously misunderstood my points, which were, in a nutshell: For applications and similar, just use whatever suits you; for operating systems do experiments in lab/research projects (as was done with Unix), because existing and established ones are relied upon by lots of software. Just to make that perfectly clear, that doesn't mean they should use C forever, it means they should wait for a potential replacement to reach a similar state of stability with independent standards and competing implementations.

[–] [email protected] 5 points 2 months ago

Try browsing the list of somewhat recent #CVE rated critical, as I just did to verify. A majority of them is not related to any memory errors. Will you tell all them “just use a different programming language”?

I'm sorry, but this has been repeatedly refuted:

And yes, they are telling their engineers to use a different programming language. In fact, even the NSA is saying exactly that: https://www.nsa.gov/Press-Room/News-Highlights/Article/Article/3215760/nsa-releases-guidance-on-how-to-protect-against-software-memory-safety-issues/

It doesn’t come out today, it’s been there for a long time, and it’s standardized, proven and stable.

This seems like an extremely short-sighted red herring. C has so many gaps in its specification, because it has no problem defining things as "undefined behavior" or "implementation defined", that the standard is essentially useless for kernel-level programming. The Linux kernel is written in C and used to only build with GCC. Now it builds with GCC and LLVM, and it relies on many non-standard compiler extensions for each. The effort to add support for LLVM took them 10 years. That's 10 years for a migration from C to C. Ask yourself: how is that possible if the language is so well standardized?

[–] [email protected] 12 points 2 months ago* (last edited 2 months ago) (1 children)

Is zig memory safe by design? If not, rust will "win". Large companies aren't going to hire for an unknown or unpopular memory unsafe language when they already have C or C++ - there's just no contest.

Last I read, zig didn't even have a standard string library. Unless that changes, it won't even be a viable alternative to C/C++.

Edit: I checked and got this

the Zig language, like C, entrusts memory management to humans, placing full trust in human development. Zig then provides some memory safety checks to ensure memory safety. However, it still lacks the strict compile-time guarantees of Rust’s ownership system and borrow checker. Therefore, when writing Zig code, developers need to pay more attention to potential memory safety issues and ensure that errors and exceptional situations are handled correctly.

Rust Magazine

Anti Commercial-AI license

[–] [email protected] -2 points 2 months ago (1 children)
  • Zig uses allocators, which will inform you if you are leaking memory.
  • Zig comes with defer/errdefer to simplify the resource cleanup (and for ergonomics).
  • Zig comes with Optionals to manage nulls.
  • Zig comes with slices (ptr + size) to manage all the bound-checking.
  • Zig automatically check for overflow/underflow arithmetic.
  • Zig will check for pointer alignments when casting between pointer types.

=> Zig is designed to make you do it right easily, and very hard to do it wrong.

In other words, Zig will let you be, but warn you when you are doing something wrong, where Rust is like Karen who is always screaming at you for every word you are typing.

To summarize, you really need to /want/ to fuck up to fail your memory management... If after all that you still can't manage your memory, it might be better for you to look for another carer.

Something is sure thou, Zig is very safe - just as it's safe to cut my veggies with a knife. I might cut a finger and bleed a little bit, but I will not use plastic knife "because it's safer".

Moreover; You are talking like if Rust is safe, all the time, which is not true in reality:

52.5% of the popular crates have unsafe code. Therefore, in an average real-world Rust project, you can expect a significant number of the dependent crates to have unsafe code -- Source

Basically, you're comparing a hypothetical world where Rust is always safe to a superficial glance at Zig's capabilities to claim a "winner" here.

And for the String library... Are you fucking serious? Do you want to compare the Zig's Std library versus the famously tiny Rust Std library? Really?

[–] [email protected] 0 points 2 months ago (1 children)

A crate having the unsafe keyword doesn't make the crate unsafe. The unsafe keyword just tells the compiler: "I know that what I'm trying to do may lead to memory safety issues, but I, as the programmer guarantee you that the codeblock as a whole is safe, so turn off some of your checks".

Using the unsafe keyword in rust is no much different than using a C library in rust.

[–] [email protected] 1 points 2 months ago (1 children)

It's when you're at the point of saying that unsafe is safe, it's the point where you should just shut it up kid...

[–] [email protected] 1 points 2 months ago (1 children)

I don't know why you are being so rude. I thought it was the rust community that was known for being toxic?

It's not my opinion on what the unsafe keyword means. That's its purpose. Nobody ever wants to write unsafe code on purpose. The unsafe keyword was created to allow safe programs to be created in rust that wouldn't be accepted by the strict rust compilers.

In a Venn diagram, there are 2 circles: safe programs (1) and programs that are deemed safe by the rust compiler (2).

Circle 2 is smaller than circle 1 and entirely contained inside it. However, there is no reason to not let people write programs from circle 1 that aren't in circle 2. The unsafe keyword exists to enable programmers to write those programs in rust. However, it comes with a warning, now the programmer is the one responsible for making the program inside circle 1.

[–] [email protected] 1 points 2 months ago

Ok I understand now. You are right. Thank you.

[–] [email protected] 6 points 2 months ago (1 children)

It's too early to tell.

Rust has a killer feature and a tonne of buzz, but poor ergonomics.

Zig is developing into simple elegance and wonderful interop, but has more work to do before it will be widely usable.

It's entirely possible that ideas and lessons taken from them will inspire another language that ends up eclipsing them both.

[–] [email protected] 13 points 2 months ago* (last edited 2 months ago) (1 children)

I would say at this point in time it’s clearly decided that Rust will be part of the future. Maybe there’s a meaningful place for Zig too, but that’s the only part that’s too early to tell.

If you think Zig still has a chance at overtaking Rust though, that’s very much wishful thinking. Zig isn’t memory safe, so any areas where security is paramount are out of reach for it. The industry isn’t going back in that direction.

I actually think Zig might still have a chance in game development, and maybe in specialized areas where Rust’s borrow checker cannot really help anyway, such as JIT compilers.

[–] [email protected] -3 points 2 months ago (1 children)

Okay, but...

it’s clearly decided that Rust will be part of the future.

That's not what OP asked.

If you think Zig still has a chance at overtaking Rust though, that’s very much wishful thinking.

That's not what I said.

[–] [email protected] 10 points 2 months ago* (last edited 2 months ago) (1 children)

No, OP asked for a black and white winner. I was elaborating because I don’t think it’s that black and white, but if you want a singular answer I think it should be clear: Rust.

[–] [email protected] 1 points 2 months ago* (last edited 2 months ago)

I see. You replied to me, though, with commentary that didn't fit the question I was answering or the thoughts I was expressing. Don't you think it would have made more sense as a reply to OP?

load more comments
view more: next ›