`IndexMut` problems for recursive maps

⚓ Rust    📅 2025-10-23    👤 surdeus    👁️ 3      

surdeus

I have a data structure which consists of recursive maps.
How can I look into this data structure with indexes?

As in:

pub struct Files {
    pub folders: HashMap<&'static Path, Files>,
    pub files: HashSet<&'static Path>,
}

impl Files {
    pub fn move_layer_up(&mut self, path: &'static Path) { }
}

fn main() {
    ...
    let p = |name| Path::new(name);

    // Works, but ugly
    files
        .folders
        .get_mut(p("headers"))
        .unwrap()
        .folders
        .get_mut(p("vostok"))
        .unwrap()
        .move_layer_up(p("__root"));

    // Doesn't work, cannot borrow data in an index
    files.folders[p("headers")].folders[p("vostok")].move_layer_up(p("__root"));
}

4 posts - 2 participants

Read full topic

🏷️ Rust_feed