Task:

 Generate a string with   N   opening brackets   [   and with   N   closing brackets   ],   in some arbitrary order. 
 Determine whether the generated string is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.


Examples

   (empty)      OK
   []           OK   
   [][]         OK   
   [[][]]       OK 
   ][         NOT OK
   ][][       NOT OK
   []][[]     NOT OK



extern crate rand;
 
trait Balanced {
    /// Returns true if the brackets are balanced
    fn is_balanced(&self) -> bool;
}
 
impl<'a> Balanced for str {
    fn is_balanced(&self) -> bool {
        let mut count = 0;
 
        for bracket in self.chars() {
            let change = match bracket {
                '[' => 1,
                ']' => -1,
                _ => panic!("Strings should only contain brackets")
            };
 
            count += change;
            if count < 0 { return false; }
        }
 
        count == 0
    }
}
 
/// Generates random brackets
fn generate_brackets(num: usize) -> String {
    use rand::random;
 
    (0..num).map(|_| if random() { '[' } else { ']' }).collect()
}
 
fn main() {
    for i in (0..10) {
        let brackets = generate_brackets(i);
 
        println!("{}    {}", brackets, brackets.is_balanced())
    }
}