Created
March 17, 2014 03:59
-
-
Save shadowmint/9593771 to your computer and use it in GitHub Desktop.
Debug'd random reader
This file contains hidden or 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 std::io; | |
use std::num; | |
use std::io::BufferedReader; | |
use rand::{task_rng, Rng}; | |
fn main() -> () { | |
let mut rng = rand::task_rng(); | |
let num: int = (rng.gen::<int>() % 9) + 1; | |
let mut reader = BufferedReader::new(io::stdin()); | |
print!("Guess a number: "); | |
let mut input: &~str = &~""; | |
let mut num_inp: int = 0; | |
while num_inp != 9 { | |
let y:Option<~str>; | |
match reader.read_line() { | |
Ok(x) => { | |
println!("<- {:?}", x); | |
y = Some(x.clone()); | |
println!("-> {:?}", y); | |
}, | |
Err(err) => { | |
println!("Bad input! Try again"); | |
y = None; | |
} | |
}; | |
match y { | |
Some(z) => { | |
println!("Read: {:?}", z.slice(0, 1)); | |
match z.slice(0, 1) { | |
"1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" => { | |
println!("Matched to a selected number"); | |
match from_str::<int>(z.slice(0, 1)) { | |
Some(y) => { | |
num_inp = y; | |
println!("The value of num_inp is now: {:?}", num_inp); | |
}, | |
None => { | |
println!("That didn't seem to be a number"); | |
} | |
} | |
}, | |
_ => {} | |
}; | |
}, | |
None => {} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment