diff --git a/src/lib.rs b/src/lib.rs index c818158..802b59e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,22 +1,14 @@ pub fn brackets_are_balanced(string: &str) -> bool { - use std::collections::HashMap; - - let pairs: HashMap = [(')', '('), ('}', '{'), (']', '[')] - .iter().cloned().collect(); let mut stack = vec![]; for char in string.chars() { match char { '(' | '{' | '[' => stack.push(char), - ')' | '}' | ']' => { - if let Some(left) = stack.pop() { - if let Some(right) = pairs.get(&char) { - if left != *right { return false } - } - } else { return false } - }, - _ => {}, + ')' => if stack.pop() != Some('(') { return false }, + '}' => if stack.pop() != Some('{') { return false }, + ']' => if stack.pop() != Some('[') { return false }, + _ => {}, } } stack.is_empty()