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_distr::Binomial;
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let args = env::args().skip(1).collect::<Vec<String>>();
|
||||
let options = Options::from_args(&args);
|
||||
let options = Options::from_args(&args)?;
|
||||
guess(&options)
|
||||
}
|
||||
|
||||
@@ -16,7 +17,7 @@ struct 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();
|
||||
while let Some(arg) = args.next() {
|
||||
match arg.as_str() {
|
||||
@@ -24,20 +25,14 @@ impl Options {
|
||||
"--max" | "-m" => {
|
||||
let max_value = args
|
||||
.next()
|
||||
.expect("Number proceeding --max should be a number");
|
||||
self.max = max_value
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("Number proceeding --max should be a number");
|
||||
.ok_or("Number proceeding --max should be a number")?;
|
||||
self.max = max_value.trim().parse()?;
|
||||
}
|
||||
"--min" => {
|
||||
let min_value = args
|
||||
.next()
|
||||
.expect("Number proceeding --min should be a number");
|
||||
self.min = min_value
|
||||
.trim()
|
||||
.parse()
|
||||
.expect("Number proceeding --min should be a number");
|
||||
.ok_or("Number proceeding --min should be a number")?;
|
||||
self.min = min_value.trim().parse()?;
|
||||
}
|
||||
_ => panic!("Unrecognized argument {:?}", arg),
|
||||
}
|
||||
@@ -45,12 +40,13 @@ impl Options {
|
||||
if self.max < self.min {
|
||||
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();
|
||||
opts.parse(args);
|
||||
opts
|
||||
opts.parse(args)?;
|
||||
Ok(opts)
|
||||
}
|
||||
|
||||
fn new() -> Options {
|
||||
@@ -73,7 +69,7 @@ fn random_binomial_range(min: u32, max: u32) -> u32 {
|
||||
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 {
|
||||
true => random_binomial_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<{}",
|
||||
opts.min, opts.max
|
||||
);
|
||||
io::stdin()
|
||||
.read_line(&mut input_string)
|
||||
.expect("Could not read string");
|
||||
io::stdin().read_line(&mut input_string)?;
|
||||
|
||||
let input_number = input_string
|
||||
.trim()
|
||||
.parse::<u32>()
|
||||
.expect("Could not parse input");
|
||||
let input_number = input_string.trim().parse::<u32>()?;
|
||||
|
||||
if input_number == to_guess {
|
||||
println!("You got it right!");
|
||||
} else {
|
||||
println!("The correct number was {}!", to_guess);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user