Mutating parts of a mutable structure

⚓ Rust    📅 2026-03-02    👤 surdeus    👁️ 1      

surdeus

Mutating values held in a Hashmap.
I have a structure RoloGuiState. Which has a

rolodex: Option<RolodexBase>.

RolodexBase has a bunch of content. For now, the one that matters is a Vec.

    mut guistate: &mut RoloGuiState,
    message: CategoryMessage,
) -> iced::Task<Message> {
...
if let Some(dex) = &mut guistate.rolodex {
                guistate.database_dirty = true;
                match cat {
                    CategoryType::Short => dex.del_short_cat(instr),

Which results in the error:

dex` is a `&` reference, so it cannot be borrowed as mutable

This worked when I was using RefCell / Rc. So how do I mutate the vectors without the RefCell / Rc?
For completeness, when I had RefCell / Rc, I used just dex rather than (*dex), and I had a clone, which for a an Rc I knew wouldn't mess anything up. And I had tested it and it worked.
Thanks,
Joel

8 posts - 2 participants

Read full topic

🏷️ Rust_feed