Using the Result type
This commit is contained in:
40
src/main.rs
40
src/main.rs
@@ -1,11 +1,12 @@
|
|||||||
use rand::RngExt;
|
use rand::RngExt;
|
||||||
use rand_distr::Binomial;
|
use rand_distr::Binomial;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let args = env::args().skip(1).collect::<Vec<String>>();
|
let args = env::args().skip(1).collect::<Vec<String>>();
|
||||||
let options = Options::from_args(&args);
|
let options = Options::from_args(&args)?;
|
||||||
guess(&options)
|
guess(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,7 +17,7 @@ struct Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Options {
|
impl Options {
|
||||||
fn parse(&mut self, args: &Vec<String>) {
|
fn parse(&mut self, args: &Vec<String>) -> Result<(), Box<dyn Error>> {
|
||||||
let mut args = args.iter();
|
let mut args = args.iter();
|
||||||
while let Some(arg) = args.next() {
|
while let Some(arg) = args.next() {
|
||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
@@ -24,20 +25,14 @@ impl Options {
|
|||||||
"--max" | "-m" => {
|
"--max" | "-m" => {
|
||||||
let max_value = args
|
let max_value = args
|
||||||
.next()
|
.next()
|
||||||
.expect("Number proceeding --max should be a number");
|
.ok_or("Number proceeding --max should be a number")?;
|
||||||
self.max = max_value
|
self.max = max_value.trim().parse()?;
|
||||||
.trim()
|
|
||||||
.parse()
|
|
||||||
.expect("Number proceeding --max should be a number");
|
|
||||||
}
|
}
|
||||||
"--min" => {
|
"--min" => {
|
||||||
let min_value = args
|
let min_value = args
|
||||||
.next()
|
.next()
|
||||||
.expect("Number proceeding --min should be a number");
|
.ok_or("Number proceeding --min should be a number")?;
|
||||||
self.min = min_value
|
self.min = min_value.trim().parse()?;
|
||||||
.trim()
|
|
||||||
.parse()
|
|
||||||
.expect("Number proceeding --min should be a number");
|
|
||||||
}
|
}
|
||||||
_ => panic!("Unrecognized argument {:?}", arg),
|
_ => panic!("Unrecognized argument {:?}", arg),
|
||||||
}
|
}
|
||||||
@@ -45,12 +40,13 @@ impl Options {
|
|||||||
if self.max < self.min {
|
if self.max < self.min {
|
||||||
panic!("min > max, wtf are you even talking about?");
|
panic!("min > max, wtf are you even talking about?");
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_args(args: &Vec<String>) -> Options {
|
fn from_args(args: &Vec<String>) -> Result<Options, Box<dyn Error>> {
|
||||||
let mut opts = Options::new();
|
let mut opts = Options::new();
|
||||||
opts.parse(args);
|
opts.parse(args)?;
|
||||||
opts
|
Ok(opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new() -> Options {
|
fn new() -> Options {
|
||||||
@@ -73,7 +69,7 @@ fn random_binomial_range(min: u32, max: u32) -> u32 {
|
|||||||
rand::rng().sample(bin) as u32 + min
|
rand::rng().sample(bin) as u32 + min
|
||||||
}
|
}
|
||||||
|
|
||||||
fn guess(opts: &Options) {
|
fn guess(opts: &Options) -> Result<(), Box<dyn Error>> {
|
||||||
let to_guess: u32 = match opts.use_normal {
|
let to_guess: u32 = match opts.use_normal {
|
||||||
true => random_binomial_range(opts.min, opts.max),
|
true => random_binomial_range(opts.min, opts.max),
|
||||||
false => random_uniform_range(opts.min, opts.max),
|
false => random_uniform_range(opts.min, opts.max),
|
||||||
@@ -84,18 +80,14 @@ fn guess(opts: &Options) {
|
|||||||
"Enter a number in the range {}<=guess<{}",
|
"Enter a number in the range {}<=guess<{}",
|
||||||
opts.min, opts.max
|
opts.min, opts.max
|
||||||
);
|
);
|
||||||
io::stdin()
|
io::stdin().read_line(&mut input_string)?;
|
||||||
.read_line(&mut input_string)
|
|
||||||
.expect("Could not read string");
|
|
||||||
|
|
||||||
let input_number = input_string
|
let input_number = input_string.trim().parse::<u32>()?;
|
||||||
.trim()
|
|
||||||
.parse::<u32>()
|
|
||||||
.expect("Could not parse input");
|
|
||||||
|
|
||||||
if input_number == to_guess {
|
if input_number == to_guess {
|
||||||
println!("You got it right!");
|
println!("You got it right!");
|
||||||
} else {
|
} else {
|
||||||
println!("The correct number was {}!", to_guess);
|
println!("The correct number was {}!", to_guess);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user