Basic Error Handling question: generic error type in return

I want to parse the second command line argument, parse it and return it as a Result<f32, Error> type. As you can see, the error type itself is not known in compile time. How can I write this function?

use std::error::Error;  
fn parse_income() -> Result<f32, Box<dyn Error>> {  

This code obviously won't compile primarily because nth() returns and option type. But I want to throw a NoneError in that case. Hence the compiler says:

expected type std::result::Result<_, std::boxed::Box<(dyn std::error::Error + 'static)>>
found type std::result::Result<_, std::num::ParseFloatError>

This concerns me because the Box<dyn Error> is not acting like a generic error type.

NoneError isn't an "error", because mixing of returning Result with Option was controversial. If you convert Option to Result, then it works:

Ok(std::env::args().nth(1).ok_or("missing arg")?.parse::<f32>()?) 
1 Like


Thanks for that, I've changed the function but the second problem persists.

You don't need to call into(), because ? does that already:

1 Like