4 ms·
I love the ideas behind Rust. I don't think it can replace C for all use-cases. C has the use-case of zero-dynamic-allocations. AFAIK, Rust is not very compati
by Peaker 11y ago
I love the ideas behind Rust. I don't think it can replace C for all use-cases.
C has the use-case of zero-dynamic-allocations. AFAIK, Rust is not very compatible with that mode of use.
- scott_s 11y agoAs far as I know, C and Rust give you the same opportunities to work with stack-allocated values.
- Peaker 11y agoIt's not just stack allocations, it's support for intrusive allocations, pointers not always conforming to simple ownership semantics, etc.
- catnaroek 11y agoHuh? If you don't want any dynamic allocation (or any other features that require support from a runtime system), you can use `#![no_std]` and instead use `libcore` directly. This is how the standard library itself is implemented.
- Peaker 11y agoThe problem, as I understand it, is that Rust's ownership semantics don't accommodate for the kinds of programming needed by 0 dynamic allocations. Lots of intrusive nodes pointing freely at one another, is an example. I've heard some Rust libraries approximate intrusive allocations almost perfectly, but not quite perfect.
- steveklabnik 11y agoAs long as you're willing to use unsafe, you can write things exactly as you can as in C. That's of course, not as satisfying as not needing to, but by encapsulating that in a library and providing a safe interface, downstream users still get the benefits.
- Peaker 11y agoCan a safe interface always be provided?
- steveklabnik 11y agoI can't say that I'm able to speak to every single possible program ever, but at _some_ level, it should end up safe, yeah.
- catnaroek 11y agoRust's ownership and borrowing rules are entirely a compile-time thing: they just determine what objects can be safely used, and when. There is absolutely no need to use dynamic allocation to benefit from Rust's ownership and borrowing checks. And none of the other core language facilities (primitive types, unsafe pointers, fixed-size arrays) require dynamic allocation either. What does require dynamic allocation is using boxed objects and dynamically sized collections (such as vectors and tree maps), which are provided by `libstd`, but not by `libcore`. Think of using `libcore` as using C or C++ in freestanding mode.
- Peaker 11y agoAs far as I know, nobody managed to create a comprehensive intrusive data structure library that behaves as optimally as in C, due to the ownership semantic restrictions.
- catnaroek 11y agoSo just bypass the ownership checks, with `unsafe`. It gives up on memory safety, but you still keep all the other benefits of a modern language: algebraic data types, pattern matching exhaustiveness checks, parametric polymorphism, etc. So, even when writing `unsafe` code, Rust is strictly superior to C and C++. Also, I would contend that, for the vast majority of programs, the benefits of a simple, hierarchical, statically enforceable resource management scheme like RAII outweigh the downsides. Programs that need more sophisticated approaches are a very small minority.