Created
October 13, 2015 06:19
-
-
Save RustyRails/45c94d445bd855c0101f to your computer and use it in GitHub Desktop.
Challenge #236 [Easy] Random Bag System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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