by thesuperbigfrog · 3y ago · 0 comments
C is the simplest of C, C++, or Rust and most other languages have C interop or foreign function interface.
First learn C and then you can learn Rust or C++ if needed. The concepts you learn in C programming will help you to better understand the more advanced concepts used in Rust or C++.
Recommended first book:
So, IMO worth jumping to Rust as soon as you feel confident with the stack vs the heap, use-after-frees, etc.
I mean, why would I expect the parenthesised subtraction to do anything I can rely on? Or did you mean (&ys[2] - &ys[0]) ?
int zs[20] = {0};
int *xs = &zs[0], *ys = &zs[10];
then no perplexing behavior is observed.Of course, any veteran C programmer will point at that being UB, but the big-array-of-bytes (with holes) model is what one sees in userspace assembly, and what I assume most systems programmers think about most of the time, rather than the C memory model.
I wouldn't recommend that someone trying to learn systems programming (as opposed to C or C++ as languages) spend, like, any time whatsoever memorizing the list of C UBs (and then learning about pointer provenance, exposed addresses, etc., I suppose?). Assembly, Forth, safe Go, and safe Rust don't have the same kind of UB, and (Linux) kernel C has a different set of UBs than userspace/spec-compliant C.
OP is asking about a language to learn. Let them fail, to learn and understand.
And after all, C was and actually is moving the world forward, and thanks to C you have the possibility to write and run programs in Rust. Rust can't do anything if not by calling C APIs. Removing C code from any system is removing the legs from the crab.
So why not choosing C? Seems wise to me. Think about the thousands of lines of C code executing across the world so you can just download a crate. It seems to me that your first interest, as a Rust user, is to have C programmers around so you can run your Rust code.
I'll contribute by telling my personal experience after 20 years of embedded and system C: the fear spread by Rustaceans (regarding UB) is uber-exaggerated, specially at learning stages.
Before you bring exploits up, not every C application is connected to internet or has a user interface or runs as root. Just a portion.
struct edge {
struct node* src;
int dst_offset;
int label;
};
Knowing about integer overflow, the original programmer carefully wrote overflow checks where dst_offset gets computed, and the code was correct. Nodes were allocated into one contiguous array, and edges were allocated into another.Later, someone else changed how nodes were allocated, so adding a new node would never trigger a realloc of the whole graph. Instead, a linked list of node arrays is allocated. Suddenly now, computing dst_offset is UB, and yet, the observed behavior of the resulting program is the same.
Even later, someone updated GCC on the CI machine, and now the inliner is more aggressive. Suddenly, mutations to node labels are unreliable, and edges are found to not refer to any node in the graph, except when debug logging logs the address they actually refer to.
This is the kinda thing that requires an understanding of UB that has nothing to do with the actual hardware to diagnose and fix, which I think is really unnecessary for a beginner.
C forces you to think like a programmer. Once you grasp that, you can learn most other languages with relative ease.
Maybe not counting Lisps.
The rust programmers that learned C first always have very elegant solutions and have the least issues in their unsafe blocks.
C to learn how memory works and how to manage resources, then Rust to understand how to use a higher level systems language for more productivity in places where it helps.
I work as a C++ dev and there are jobs in it, but unless you are trying to work a C++ job, the other two are wayyy better.
C knowledge also helps to understand what rust's borrow checker is actually doing. And it will teach you what Box / Rc / etc are for. Its surprisingly easy to write rust code that Box everywhere, and runs very slowly in practice. (I've seen rust programs run slower than their javascript equivalents). Learning C first will give you the right knowledge base to appreciate rust and use it well.
However, it is not a productive systems programming language these days compared to C++, Rust, or Zig.