Modeling a file system
I've been teaching myself programming primarily using Rust, and it's been a blast. I seriously enjoy its modern ergonomics and the feeling of safety it gives me. But I'm running into a situation where I think I have to use smart pointers, and that's a problem because I've never used them before, and reading about them in the book just kind of breaks my brain.
What I want to do is abstractly represent a file system, with a root directory that contains files and subdirectories. I tried to do this:
struct Directory<'a> {
parent: Option<&'a Directory<'a>>,
directories: HashMap<String, Directory<'a>>,
files: HashMap<String, usize>,
}
Which compiles, but immediately runs into issues when I want to define methods to do things like insert subdirectories. I get a cavalcade of compiler errors that I don't have the first idea how to fix. In my mind, there shouldn't be an issue with dangling references, since the parent optional reference should only point to the parent, which owns the child. I guess the compiler can't prove this?