Remix.run Logo
matltc a day ago

I've spent a good amount of time with C, nowhere near mastery though. Is it worth still writing C, or better off just learning Rust if my goal is to write embedded/systems code?

Aurornis a day ago | parent | next [-]

Rust is worth learning, but your C skills will continue to be useful for embedded for a long time. Rust support in the embedded world is still growing and you will find yourself going between Rust and C on most projects unless you can carefully pick your platform for Rust support up front.

Coming from C I don’t think you’ll find Rust too foreign, once you internalize how the ownership rules work. In my experience the formal rules of Rust overlap a lot with behaviors that are good practice in C/C++ anyway, but there are some complicated concepts that you need to wrap your head around before expressing them in Rust becomes second nature.

pornel a day ago | parent | prev | next [-]

Technically, absolutely.

Whether it would suit you, depends if you can learn to like Rust's approach of moving more work to the type system. In Rust you do certain things the Rust's way, period. Programmers used to C being unopinionated about everything find that objectionable.

uecker a day ago | parent | next [-]

C is a tool which requires expertise but then goes out of your way and let's you do things, and do things rather efficiently, with no overhead, and exactly how you want. If you want it to cut off your arm it will do this too. But if you want to abstract things away behind types, this can also be done too (and arguable should be done more often in C). Somebody should write a C to more modern and safe C migration book.

pornel a day ago | parent [-]

Depends how you define "can", because you can invent your own conventions not checked by the compiler, or even make a compile-to-C language.

But more directly, C barely lets you define non-NULL pointers. It doesn't have pointers that guarantee the data behind them is initialized, it doesn't have never-leaves-this-thread data types. Const merely guarantees that you can't (strongly shouldn't) mutate data, not that it definitely won't be mutated by any thread.

uecker 15 hours ago | parent [-]

Most of this can be done just fine in practice. NULL safety is not so much an issue as people claim as trapping is safe on most implementations and then similar to a panic in other languages. If you create data types via constructors you can then make sure those are initialized. Yes, this is a convention but easy to check. The const issue is not a problem IMHO. If you violate type safety yourself then this comparable to using escape hatches in other languages. Only the never-leave-this-thread point is something where Rust truly has an advantage, but one can have abstract data types that are thread-safe in C.

This is why I think a book is missing, people think C is much worse than it is because they do not understand what can be achieved.

matltc a day ago | parent | prev [-]

> moving more work to the type system

Sounds right up my alley. Thanks to you (and other siblings) for the thoughtful replies.

cryo32 a day ago | parent | prev | next [-]

Depends exactly what you mean by embedded but Rust isn’t as common as people make it out to be there. In some aread it’s almost entirely unheard of. C will be king for a while yet. There’s a lot of Ada floating around too.

greenavocado a day ago | parent | prev | next [-]

Depends on if your embedded system has a nice toolchain for it and/or how much suffering you are willing to endure. Another consideration is if you have to collaborate with others. Most programmers don't know Rust.

0x457 a day ago | parent | prev [-]

> embedded/systems code?

Depends on platform for embedded. It not very pleasant to write rust if you have to think about binary size. For systems code - sure, use rust.