Skip to content

Instantly share code, notes, and snippets.

@RustyRails
Created October 13, 2015 06:19
Show Gist options
  • Save RustyRails/45c94d445bd855c0101f to your computer and use it in GitHub Desktop.
Save RustyRails/45c94d445bd855c0101f to your computer and use it in GitHub Desktop.
Challenge #236 [Easy] Random Bag System
extern crate rand;
use rand::Rng;
use std::option::Option;
struct Bag {
contents: Vec<char>,
rng: rand::ThreadRng,
}
fn new_bag() -> Bag {
Bag {
contents: Vec::new(),
rng: rand::thread_rng(),
}
}
impl Iterator for Bag {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
if !self.contents.is_empty() {
self.contents.pop()
} else {
self.contents = vec!['O', 'I', 'S', 'Z', 'L', 'J', 'T'];
self.rng.shuffle(&mut self.contents);
self.contents.pop()
}
}
}
fn main() {
let mut bag = new_bag();
for c in (&mut bag).take(50) {
print!("{}", c);
}
print!("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment