Skip to content

Commit

Permalink
main: Change return type to Result<(), &'static str>
Browse files Browse the repository at this point in the history
Instead of panicking in situations where execution was not successful,
it's generally more productive to properly print an error and exit with
a non-zero status code.  (Panics are generally not good UX, and should
really be reserved for cases where we're fully "out of ideas".)

First, adjust the return signature of main away from returning () (the
default) towards instead returning a static, hard-coded str type.  I may
need to revisit the exact type, but this is the simplest I can come up
with.

Signed-off-by: Kristofer Rye <kristofer.rye@gmail.com>
Tested-by: Kristofer Rye <kristofer.rye@gmail.com>
  • Loading branch information
rye committed Aug 5, 2019
1 parent 329d795 commit 0afe903
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
}
}

fn main() {
fn main() -> Result<(), &'static str> {
let max_n: u32 = 10;

let matches = App::new(env!("CARGO_PKG_NAME"))
Expand All @@ -48,10 +48,11 @@ fn main() {
command.arg(arg);
}

command
Some(command)
}
_ => panic!("no command was given"),
};
_ => None,
}
.ok_or("no command was given")?;

let mut iterations: u32 = 0;
let mut slot_time: Option<SlotTime> = None;
Expand All @@ -65,12 +66,9 @@ fn main() {
iterations += 1;

match status.code() {
Some(0) => break,
Some(0) => break Ok(()),
Some(_) => {}
None => {
eprintln!("Child terminated by signal; breaking");
break;
}
None => break Err("child terminated by signal"),
}

if let Some(SlotTime::AutoGenerated(dur)) = &slot_time {
Expand Down

0 comments on commit 0afe903

Please sign in to comment.