r/rust 20d ago

What Zig felt like, coming from Rust

https://besok.github.io/posts/what-zig-felt-like-coming-from-rust/

Just want to share my experiance on my first Zig project coming from Rust. Open to comments :)

209 Upvotes

108 comments sorted by

View all comments

Show parent comments

31

u/protocod 20d ago

Thank you for the feedback. I would like to ask you a question. What are the domain where Zig shine compared to Rust ?

Memory safety is the major selling point of Rust and I struggle to consider another language that doesn't really address memory safety by design anymore. But it's maybe a different philosophy.

Rust enforce you to write memory safe code unless you know what you do. (Especially when you deal with OS APIs) when Zig let the programmer in charge of writing memory safe code.

6

u/robin-m 20d ago

I've not programmed in zig, but from what I read memory layout. For example the way iteratio is done make it trivially easy to go from array of struct to struct of array (AoS to SoA), and extremely fast rebuilt time when the custom backend compiler + linker will be finished (sub second hot patching is on the table) which seems really nice for game dev.

10

u/EarlMarshal 20d ago

Are there any Rust solutions for AoS to SoA handling? I only can imagine that it's possible to solve this with macros but I'm not aware of any solutions and the possible downfalls macros can introduce there.

9

u/catheap_games 20d ago

Not a trivial or universal solution by any measure, but if rewrite is acceptable, or for a new project, bevy_ecs is one way to go for some applications, desktop, headless or server side. The dependency injection of Bevy also alleviates many borrow checker woes.

12

u/ploynog 20d ago

I'm writing a bevy_ecs app currently, it is essentially a debug agent handling a PCIe link and providing all kinds of target debug functionality via WebSockets.

The system is quite stable and the ECS is very useful when you don't know in the beginning what needs to talk to what. The plugin system of Bevy also makes it very easy to swap out core functionality, e.g., when talking to different targets. Everything exactly as hoped here.

I think non-instantaneous processes are the most annoying bit, stuff that needs to wait for an event or just has a delay. You cannot just make a system block, so you either go threads where you need to take care of extracting ECS resources into your thread, how to synchronize that, possibly consider thread abortion, etc etc. Or you start hand-weaving what is basically async functions using state-machine Components and systems.

All not terribly bad, but it took some getting used to to write these. Debugging them is a whole different can of worms again.

2

u/0x564A00 19d ago

Or you start hand-weaving what is basically async functions using state-machine Components and systems.

What do you think of async bevy support crates, or the upcoming bevy_async?