diff --git a/src/main.rs b/src/main.rs index 0c25b5a..127e390 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,19 +2,33 @@ fn main() { let args = vec![ - "string1", - "string2", + "(){}[]", + "([{}])", + "(}", + "[(])", + "[({})](]", ]; println!(); for arg in args { - println!("{}",func(&arg)); + println!("{:8} => {}",arg, valid_braces(arg)); }; println!(); } -fn func(s: &str) -> String { - s.into() +fn valid_braces(s: &str) -> bool { + let mut v = vec![]; + + for c in s.chars() { + match c { + '('|'{'|'[' => v.push(c), + ')' => if v.pop() != Some('(') { return false }, + '}' => if v.pop() != Some('{') { return false }, + ']' => if v.pop() != Some('[') { return false }, + _ => {}, + } + } + v.is_empty() }