use std::collections::HashMap; use std::hash::Hash; #[derive(Default)] pub struct LayeredEnv { parent: Option>>, inner_map: HashMap, } impl LayeredEnv { pub fn insert(&mut self, key: K, value: V) { self.inner_map.insert(key, value); } pub fn push(self) -> LayeredEnv { LayeredEnv { parent: Some(Box::new(self)), inner_map: HashMap::new(), } } pub fn pop(self) -> LayeredEnv { *self.parent.unwrap() } pub fn lookup(&self, key: &K) -> Option<&V> { match self.inner_map.get(key) { Some(v) => Some(v), None => match self.parent.as_ref() { Some(p) => p.lookup(key), None => None, }, } } }