3 ms·
The intro may give an impression that Rust has less control about moving values than C, but that's not accurate. The problem is that both languages behave exact
by pornel 4y ago
The intro may give an impression that Rust has less control about moving values than C, but that's not accurate. The problem is that both languages behave exactly the same: they can pass and assign structs by value, which changes their address.
In C any problems caused by this are "solved" just by telling programmers not to do that. In Rust that's not good enough — safety guarantees are supposed to catch such violations, so the goal is to automatically and robustly prevent incorrect struct moves. Rust already makes that safe by default when using the borrow checker, but it doesn't have a nice built-in mechanism for preventing moves of non-borrowed structs.
- xani_ 4y agoPretty much. In Rust you have to convince compiler what you're doing makes sense or tell compiler to fuck off, in C it's pretty much "it's your funeral, whatever dude"
- im3w1l 4y agoIn Rust you have to create and initialize an object on the stack and then move it to the heap. This is not necessary in C.
- pornel 4y agoThe usual safe way is to do that, and count on LLVM to eliminate the copy. However, Rust also has a MaybeUninit wrapper type for allocating uninitialized memory wherever you want and writing to it directly.