Hello!
In a piece of code, I needed to modify a value in self (a struct). However, for some reason it will not change.
#[derive(Clone, Debug)]
pub struct SymbolTable {
pub symbols: HashMap<String, Symbol>,
pub parent : Box<Option<SymbolTable>>
}
impl SymbolTable {
pub fn get(&mut self, name: String) -> Option<Symbol> {
println!("{:#?}", self.symbols); // Shows an empty HashMap
match self.symbols.get(name.as_str()) {
Some(value) => return Some(value.clone()),
None => return None
}
}
pub fn set(&mut self, name: String, value: types::Type) {
self.symbols.insert(
name,
Symbol {value}
);
println!("{:#?}", self.symbols); // Shows a hashmap with a symbol in it.
}
}
I am running:
symbols.set("x".to_string(), myvalue);
symbols.get("x".to_string());
It does compile, it does set, but only locally so the get method says it didn't find the symbol.
Please let me know how to get it to set globally.