From 702b2010f2741da5380aaeb7c26e7850dea6c9bd Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 19 Mar 2020 20:55:49 +0800 Subject: [PATCH] Add contains to Scope. --- src/scope.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/scope.rs b/src/scope.rs index dcd35091..b7d43c97 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -139,6 +139,16 @@ impl<'a> Scope<'a> { self.0.truncate(size); } + /// Does the scope contain the variable? + pub fn contains(&self, key: &str) -> bool { + self.0 + .iter() + .enumerate() + .rev() // Always search a Scope in reverse order + .find(|(_, ScopeEntry { name, .. })| name == key) + .is_some() + } + /// Find a variable in the Scope, starting from the last. pub fn get(&self, key: &str) -> Option<(usize, &str, VariableType, Dynamic)> { self.0