fn is_valid(s: String) -> bool { let mut stack = Vec::new(); for i in s.chars() { match i { '{' => stack.push('}'), '(' => stack.push(')'), '[' => stack.push(']'), '}'|')'|']' if Some(i) != stack.pop() => return false, _ => (), } } return stack.is_empty(); } fn main() { println!("{}", is_valid("()[]{}".to_string()) ) }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter