1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
pub fn split_balanced(s: &str, split_character: char) -> Vec<&str> { s.split(balanced(split_character)) .map(str::trim) .collect::<Vec<_>>() } fn balanced(character_to_match: char) -> impl FnMut(char) -> bool { let mut num_parens = 0; move |c| match c { '{' => { num_parens += 1; false } '}' => { num_parens -= 1; false } other => num_parens == 0 && other == character_to_match, } }