3 ms·
Is it really different from C though? (But with some runtime intervention instead of UB) Rc and Arc are maintaining the “be sure that you'll only free this onc
by littlestymaar 5y ago
Is it really different from C though? (But with some runtime intervention instead of UB)
Rc and Arc are maintaining the “be sure that you'll only free this once” invariant automatically. In C you have the same invariant, you just have have to maintain it by yourself (or use an ad hoc reference counting mechanism) and if you don't it's a double free (UB). And in both cases you are responsible ensure the memory is freed at least once.
Mutex ensure you don't perform one read and one write at the same time. This is also forbidden by the C memory model and you'll probably end up using a mutex too (or other concurrency primitives) with the exact same risk of deadlock.
- flohofwoe 5y agoA lot of useful C programs can be written without dynamic memory allocation or multithreading though, while Rust seems to assume a sort of "base complexity" even for very simple programs as soon as memory access is involved.
- drran 5y ago300 byte Blink for Arduino: https://github.com/vlisivka/rust-arduino-blink https://github.com/vlisivka/rust-arduino-blink Primitive pin tester for Arduino in 1kb: https://github.com/vlisivka/rapt https://github.com/vlisivka/rapt
- tialaramex 5y agoRust doesn't assume an allocator. The Rust standard library (to have things like growable strings) requires an allocator but the language and its core do not. And Rust doesn't assume threading either. The default stuff isn't even thread safe, it's just that in Rust when you introduce threading and still try to use stuff that isn't thread safe the compiler will tell you that can't work. You can deliberately write stuff that won't do what you meant of course, but they're trying to make it harder to do that by accident. I have a set of Rust types which defy logical behaviour (for example type whose members always compare as equal yet hash differently) and as intended if you use those in otherwise safe code your program might not work but it doesn't cause random craziness. For example maybe your attempt to binary search an ordered list of my data types that claim they form a total order and yet always reply "I'm smaller" when asked how they compare to anything, even themselves - will become an infinite loop, but it won't suddenly execute environment variables as code or anything like that whereas in C all bets are off.
- littlestymaar 5y ago> And Rust doesn't assume threading either. The default stuff isn't even thread safe Right, and Rust doesn't even know what a thread is actually. It's all implemented in the standard library. The only thing Rust know, is that there exist some special Traits (Send and Sync) which are automatically implemented if a struct field implements it itself. And that's it.
- littlestymaar 5y agoNot at all. (And that's actually the big reason why Rust is making its way to the Linux Kernel)
- moonchild 5y agoC allows much more flexibility wrt lifetimes than does rust. For example: a common pattern in video games is the frame-local allocation. You (cheaply) allocate some object at the beginning of a frame. Share references to it all over the place. At the end of the frame, you (cheaply) free all the objects that were allocated in this manner. This cannot really be expressed in terms of rust's ownership system. More generally, rust enforces local correctness, while c allows for global correctness (a weaker condition). Consider, as a simpler example, this c function which I write frequently: char *tmpstr(void) { static char buf[16][1024]; static int i = 0; char *ret = buf[i]; i = (i+1) % 16; return ret; } This function is not locally correct; in a vacuum, it is impossible to say whether this function violates invariants. (Ignoring the problem of overflowing the 1024-byte buffer.) There are many programs using this function which are correct; then, there are some programs which call this function more than 16 times in a row and end up with undesirable behaviour as a result. Rust requires that every function be locally correct. Papering over some details: for any sound function f and hole-containing program P[_], if P[f] is well-typed then P[f] is sound; that means that it's always meaningful to consider f without considering the context in which it will be called. There is no such guarantee in c. As a sound language, this is a very useful attribute for rust to have, because it means that libraries can be meaningfully type-checked (not to mention it significantly simplifies the type system), but it would be naive to say that rust simply formalizes the ownership model you would have used anyway in c.
- masklinn 5y ago> C allows much more flexibility wrt lifetimes than does rust. Because C does not have lifetimes at all, or ownership as a formal langage concept, and thus does not run head first into the completeness problem. Which is one of unsafe’s use cases: when the compiler can’t prove the code is correct (because there will always be correct programs the compiler can’t prove, and there are currently a fair bit of them), and you can, and there are no suitable safe alternatives, you use unsafe. > For example: a common pattern in video games is the frame-local allocation. You (cheaply) allocate some object at the beginning of a frame. Share references to it all over the place. At the end of the frame, you (cheaply) free all the objects that were allocated in this manner. This cannot really be expressed in terms of rust's ownership system. Of course it can, though it requires APIs which are not there (in the stdlib, yet): a frame local allocator and custom per-object allocator. All frame-local objects use that and thus keep a reference to the allocator. And it avoids storing frame-local objects in locations which outlive the frame. > This function is not locally correct; in a vacuum, it is impossible to say whether this function violates invariants. That is also a use-case for unsafe: an unsafe function is one for which the caller is responsible for checking invariants. This is unsafe anyway because modifying globals is unsafe to start with.