I was trying to implement a tree in rust. I want to do something like
self = self.children
self.children is an array of
I try to use
Rc to wrap the
Node but failed. Is there any way to do it or is it a bad idea implementing a pointer tree in rust? I’ve read some posts and they said we should use array to build a tree instead.
Circular refereces are not welcome in rust. You have to use
unsafe or array/vec index to work around rust’s borrow checker.
I believe the tree structure is so common and useful that there are many implementations in crates.io. But none of the general purpose tree libraries suit my need so I wrote yet another crate: trees. Perhaps you can simply use it. Yes it’s a pointer-based implementation.
If you are trying to learn rust by implementing a tree library, the very first thing is to design the Node struct.
Note that you need to
remove node from the
children array first, because you need to guarantee it won’t be in two places at once.
It seems impossible to walk through a tree by removing its
child from its
children. Maybe I should consider implementing it in recursive way.