It's OK to return Result<(),E> in main

This commit is contained in:
Justin Kelz 2022-04-03 17:58:38 -07:00
parent c393f0e6e5
commit d279e7e191

View File

@ -8,11 +8,11 @@
use std::num::ParseIntError;
fn main() {
fn main() -> Result<(),ParseIntError> { // This isn't too bad, main should return (), so we just float the result structure up to main.
let mut tokens = 100;
let pretend_user_input = "8";
let cost = total_cost(pretend_user_input)?;
let cost = total_cost(pretend_user_input)?; // Here we're making cost a result
if cost > tokens {
println!("You can't afford that many!");
@ -20,6 +20,8 @@ fn main() {
tokens -= cost;
println!("You now have {} tokens.", tokens);
}
Ok(()) // :facepalm: So, I was on https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html at the final example.
// Wish I had read further
}
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {