News for hackers. Stuff that matters.
Re:Zig and Rust by formerly_proven on Monday March 27, 2023 @11:02AM (#35327598)
Zig feels like a good match for replacing C on microcontrollers because it permits “hold my beer” shenanigans and memory management is less of an issue. Zig metaprogramming also seems like a better match for these environments - think about how much nicer you could do stuff like qmk -, the Rust embedded stuff seems quite painful in comparison.

Haven’t tried this yet, it’s on my perennial todo list and when I touch this stuff I usually want to get something done. I also imagine you’ll have to do everything yourself with Zig here, while Rust does have a lot of HAL crates.

Re:Zig and Rust by grawp on Monday March 27, 2023 @11:32AM (#35328008)
I have the exactly opposite experience. Rust move semantics, borrowing, Send&Sync types and checks and type based compile time state-machines is exactly what I was missing specifically on embedded systems in C/C++ and what I'm sorely missing in Zig.

In Rust you start by splitting peripheries to blocks, then to registers, then to register parts and distributes these (these are zero cost operations!) into irq handlers and tasks and the type systems will check that everything that need exclusive access has it .. that you do not use read-modify-write operation on a same register from two interrupts where one can preempt the other without using some guard as mutex.. while it perfectly allow you to do this with write-only registers without guard etc... it is just so nice to work with.

People who find it complicated and/or not worth it are usually doing either some rudimentary super duper easy stuff on architectures or OSes which does not have or utilize nested interrupts or they do it on some IOT like noncritical stuff where they don't give a s** (vs industrial machinery, drones, other vehicles..).

Re:Zig and Rust by kristoff_it on Monday March 27, 2023 @11:38AM (#35328089)
> Zig feels like a good match for replacing C on microcontrollers because it permits “hold my beer” shenanigans

People who have used Zig to do embedded work have reported the opposite: they like Zig because it helps keep simple things simple and not more unsafe than they have to be.

https://kevinlynagh.com/rust-zig/

Re:Zig and Rust by bschwindHN on Monday March 27, 2023 @10:39PM (#35335169)
I'm not sure what was available to the author at the time of writing, but it feels like abstractions have gotten better and writing embedded Rust has gotten easier.

I also wrote keyboard firmware in Rust and thought it was great! I leaned on embedded-hal and the chip HAL crates (one layer of abstraction higher than the author in your link) and was able to get the majority of the firmware done in a day or two. For example, here is my key matrix scanning function:

https://github.com/bschwind/key-ripper/blob/d33db6144bbb6f80...

I also gave a small talk on the subject (aimed more at beginners) at a Rust meetup in Tokyo:

https://www.youtube.com/watch?v=x7LQevYn7d0

And finally, a (somewhat) dated article I wrote about using Rust for embedded work:

https://blog.tonari.no/rust-simple-hardware-project

Overall, it feels totally usable to me, and I love how Rust's ownership and lifetime concepts prevent me from doing stupid things like reusing a pin for two peripherals or configuring a peripheral with pins it can't work with.