Changing all instances of a value

i have the following code:

#[derive(Clone, Debug, PartialOrd, PartialEq)]
pub enum IR {
    Function(String, Vec<IR>, Vec<IR>),
    TypeInput(String),
    Struct(String, String, Vec<IR>),
    Trait(String, Vec<IR>),
    TraitFunction(String, Vec<IR>),
}

#[derive(Clone, Debug)]
pub struct VTable{
    pub traits: HashMap<String, IR>,
    pub structs: HashMap<String, IR>,
    pub functions: HashMap<String, IR>,
}

impl VTable {
    pub fn new() -> Self{
        return Self {
            included: Vec::new(),
            traits: HashMap::new(),
            structs: HashMap::new(),
            functions: HashMap::new(),
        }
    }
}
fn main(){
    let table = VTable::new();
    let ir: Vec<IR> = Vec::new();
}

in this code, i have a vector of every piece of IR, for the compilation process (this is for a programming language btw), and i have a Vtable that handles things like what traits currently exist, every struct existing etc. i want to use a data type so that whenever i chage a value in the VTable, it would also change inside the Vec. for example:

impl VTable{
    fn add_function_to_struct(&self, name: String, new: IR){
        self.structs.get(name).unwrap() = new;
    }
}
fn main(){
    let table = VTable::new();
    let ir: Vec<IR> = Vec::new();
    ir.push(IR::TypeInput(String::from("example")));
    table.insert(String::from("example"), IR::TypeInput(String::from("example")));
    table.set(String::from("example"), IR::TypeInput(String::from("another example"))
    /// the goal is that if i were to now use
    /// ir.iter().nth(0) then i would get Some(IR::TypeInput("another example"))
    /// even though the value of ir never changed directly
}

so, is there any way to do such a thing?

i forgot that RefCell exists, it's possible to just turn the VTable's type from <String, IR> into <String, Rc<RefCell<IR>>. and after doing this, using *VTable.get(String::from("example")).unwrap() = IR::Default
works fine

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.