#Trying to make a tree-like data structure with forward and backward references... help!

4 messages · Page 1 of 1 (latest)

keen basin
#

Weak pointers interact with Rc or Arc, not Boxes.

#

My advice would actually be to rethink your approach: put everything in a Vec, and then use indices instead of pointers

struct Filesystem {
  entries: Vec<Entry>
}
enum Entry {
  File(File),
  Folder(Folder),
}
struct File {...}
struct Folder {
  contents: ...,
  parent: usize, //usually root is taken to be its own parent
  children: Vec<usize>
}
#

Any scenario in which you need a graph as opposed to a tree is usually easier to deal with like this, especially if you also need mutability, else you get refcell hell.

And a tree with parent pointers is technically not a tree, it's a directed cyclic graph

acoustic brook
#

Yeah that's not a bad idea. I just wanted to see if it would be possible to do this via a graph... then I realized graphs are really tough to do in rust lol