by Const-me · 10y ago · 0 comments
IMO, Rust just not good enough for creating complex specialized data structures.
Even a trivial tree is hard to implement in Rust, and the implementation will be severely limited: https://github.com/SimonSapin/rust-forest
D surely has it’s problems as well (in short, I think the language itself is very good, but its runtime and libraries still need a lot of work), but at least it’s good for complex data structures.
I mean, you haven't actually pointed at a trivial tree implementation. The requirement that children be able to navigate to siblings is the issue here (complicating unique ownership by parents).
You could totally do a "trivial tree" as
struct Tree<T> {
payload: T,
children: Vec<Tree<T>>,
}
which your link even says. You would need a handle to the parent to surf around through siblings, is the limitation.It's not really clear (to me) that the rctree is really all that complicated; the Rc stuff is there to clarify ownership: for how long should allocated memory stick around. It might just read weirdly due to familiarity, in the same way that D might weird me out.
If you want you can always use raw pointers and dereference them in unsafe {} blocks, and even provide a safe API (return borrows with appropriate lifetimes) around this. You'll find plenty of common patterns in the standard library and on crates.io too.
For example, a vector that keeps some items + hash map that implements an index over that vector. Or some container that keeps some items + a linked list to track and evict the least recently used item from that container.
Rust is just too safe for that. That might be technically doable, but harder to implement, and harder to use.
This certainly applies to Rust’s built-in data structures as well. See this example, the problem is trivial to solve in most languages: https://www.reddit.com/r/rust/comments/3cz71e/holding_a_muta...
> See this example, the problem is trivial to solve in most
> languages
I disagree. That proposed code (again using references which provide guarantees that no other language provides) is failing because Rust statically prevents data races, which is not only non-trivial to solve in most languages, but actually impossible to solve in general in most languages. The way around this is to, again, regress to what other languages do and use raw pointers directly.About the safety, it’s important to understand there’s a price. In some cases, it causes simple things hard to implement, or cost performance.
For example, there’re algorithms out there that modify items of the same collection at the same time, both single threaded like sorting, and parallel.
Can such algorithm cause a data race? Yes.
Is it good the compiler tells me about that? Sure.
Is it good the compiler prevents me from doing that at all, and forces me to code some workarounds, that will cost me time to implement, will be ugly, and/or will sacrifice runtime performance? Not sure, I like having a choice.
I am frustrated with new languages. I agree that C++ is old and needs a more modern replacement but:
- Go is weird with no templates and no functional programming.
- Nim comes close to my dream language but has a GC and some very awkward features like globally imported namespaces.
If I had to start a new project I would probably pick Nim or C++.
Other languages do this too, for instance in Java you can override final with reflection. I lost my mind, both good and bad, when I discovered this.
It's taken me a year to feel good about my Rust code, I'm still getting a handle on unsafe though.
By “efficient” I mean I want to split my list in two for O(1) complexity, like other programming languages do.
pub struct ListNode<T> {
data: T,
prev: Weak<RefCell<ListNode<T>>>,
next: Rc<RefCell<ListNode<T>>>,
}
The code linked above probably has bugs (for sure with singleton lists), but I hope you get the idea. It allows you to splice lists in and out in constant time.Also, the code doesn't use `unsafe`. Now, you would need to use `unsafe` somewhere to actually create a list, in order to initialize the fields (or you could change the type of `next` to be `Option<...>`). However, the use of `unsafe` would be limited to that method. This tries to get at the difference between (i) occasionally using `unsafe` and (ii) having no safety guarantees at all.
There are probably lots of other pain points that you can bring up, and I'm not trying to say this should be good enough, but I thought it might help explain what is possible.