News for hackers. Stuff that matters.
Re:When Zig is safer and faster than Rust by bpolverini on Tuesday March 07, 2023 @01:03PM (#35058725)
I buy the premise that Zig is better if you know you will have lots of pointer arithmetic going on. Having written a fair amount of unsafe C interop code in Rust, I feel like these critiques of the ergonomics are valid. The new #![feature(strict_provenance)] adds a new layer of complexity, that, I hope, improves some of this experience while adding safety. Rust's benefits are not free.

The benefits of Rust's (wonderful) model around references and lifetimes come at a significant cost to ergonomics when having to go into the Mordor of some C library and back. I usually find myself wishing I could have some macro where I just write in C and have it exposed as an unsafe back in Rust. I know I can do this by just writing a C dylib and integrating that, but now I've got two problems.

Even still, I prefer writing unsafe Rust to writing C. std::mem::ptr forces me to ask the right questions and reminds me of just how easy it is to fall into UB in C as well.

Re:When Zig is safer and faster than Rust by WalterBright on Tuesday March 07, 2023 @01:11PM (#35058829)
> I usually find myself wishing I could have some macro where I just write in C and have it exposed as an unsafe back in Rust.

In D you can just import a .c file mycfile.c with:

    import mycfile; // C file filled with C functions
and they'll be treated as @system code by the D semantics. They're even inlinable.
Re:When Zig is safer and faster than Rust by swsieber on Tuesday March 07, 2023 @01:29PM (#35059096)
Here's one that does that: https://lib.rs/crates/inline-c

I'm not sure it's what you're looking for, but it seems like a good starting point.

As for the general thrust of your comment and the article, I agree. It'll be interesting to see what changes come to make things nicer.

Re:When Zig is safer and faster than Rust by zdimension on Tuesday March 07, 2023 @08:54PM (#35064174)
This one runs the C code as a program, so the code doesn't really communicate. I made a proof of concept some time ago of a macro that really translates C to Rust at compile time: https://github.com/zdimension/embed-c
Re:When Zig is safer and faster than Rust by likeabbas on Tuesday March 07, 2023 @01:29PM (#35059102)
> I usually find myself wishing I could have some macro where I just write in C and have it exposed as an unsafe back in Rust.

There’s a macro for doing this with Assembly, I can imagine one could be made for C. But why wouldn’t you just write unsafe rust at that point?

Re:When Zig is safer and faster than Rust by mjamil on Tuesday March 07, 2023 @01:55PM (#35059506)
Perhaps because you want to import something you don’t want to rewrite? There’s a lot of tested C code out there.
Re:When Zig is safer and faster than Rust by likeabbas on Wednesday March 08, 2023 @11:49AM (#35071351)
That's a good point.
Re:When Zig is safer and faster than Rust by brundolf on Tuesday March 07, 2023 @03:39PM (#35060884)
Colocation, and avoiding the ceremony of standing up all the extern types (and integrating a separate C compiler, etc)
Re:When Zig is safer and faster than Rust by Scarbutt on Tuesday March 07, 2023 @02:54PM (#35060302)
You are not alone. The other day I was checking out a new programming language and the author rewrote the unsafe rust part to zig: https://github.com/roc-lang/roc/blob/main/FAQ.md#why-does-ro...
Re:When Zig is safer and faster than Rust by xx_ns on Wednesday March 08, 2023 @12:53AM (#35065721)
This was quite literally the motive for this article.
Re:When Zig is safer and faster than Rust by Scarbutt on Wednesday March 08, 2023 @11:01AM (#35070640)
who reads articles? (joking ;) my bad)
Re:When Zig is safer and faster than Rust by Asooka on Tuesday March 07, 2023 @04:19PM (#35061393)
One big difference between unsafe Rust and C is that C compilers have flags to turn off the UB, so you have a lot less mental load when writing it. You go from e.g. "if this index calculation overflows, we may read from outside the array, because the bounds check was deleted" to "if this index calculation overflows, we may read from the wrong index, but never outside of the array bounds". UB is Damocles's sword and the speed gains are usually not worth it. With UB your program can enter a buggy state that you cannot detect, because "it cannot happen". Without UB, your program can still enter a buggy state, but you can detect it and potentially recover or crash immediately before even more things go wrong.
Re:When Zig is safer and faster than Rust by bpolverini on Tuesday March 07, 2023 @05:29PM (#35062190)
Very true and worth evangelizing to others. I have unknowingly violated -fstrict-aliasing in some part of my code only to discover later that it is benign at -O0 and metastasized at -O3.

This freaks me out the most in networking code, where there is all kinds of casting of structs (esp. if you blindly copy-paste examples from StackOverflow) and performance usually matters. Rust has inspired me to take more time to profile C code to see whether strict aliasing (strict overflow, etc.) actually make a significant enough improvement to merit the UB-risk, review time, and acid in the stomach.

Re:When Zig is safer and faster than Rust by jononor on Wednesday March 08, 2023 @02:01AM (#35066082)
Which compiler flags do I use to turn off UB?
Re:When Zig is safer and faster than Rust by wg0 on Wednesday March 08, 2023 @02:28AM (#35066247)
Are all the UBs defined/documented/catalogued somewhere?
Re:When Zig is safer and faster than Rust by pjmlp on Wednesday March 08, 2023 @02:44AM (#35066343)
Some flags to turn off some UB, and not all of them have those flags.
Re:When Zig is safer and faster than Rust by zdimension on Tuesday March 07, 2023 @08:52PM (#35064149)
Shameless self-plug: https://github.com/zdimension/embed-c

This transpiles C to Rust at compile time, though it requires a nightly compiler and hasn't been updated in some time. But it's exactly what you're looking for. C code in, unsafe Rust code out.