4 ms·
How about separate the processing code from error handling code into a block of it's own. Like this: fn file_double<P: AsRef<Path>>(file_path: P) -> Re
by extropy 11y ago
How about separate the processing code from error handling code into a block of it's own. Like this:
fn file_double<P: AsRef<Path>>(file_path: P) -> Result<i32, String> {
let mut file = File::open(file_path);
let mut contents = String::new();
let read_Result = file.read_to_string(&mut contents);
let n = contents.trim.parse::<i32>();
let result = 2 * n;
error_handling {
file: None => Err("Failed open file"),
read_Result: Err(err) => Err(err),
n: Err(err) => Err(err),
_success: Ok(result)
};
}
The compiler would then convert each error handling case into early return statements in the places where assignment is made to the specified variable.
- burntsushi 11y agoI'm not sure how that's supposed to work. For example, you have `file.read_to_string`, but in your code, `file` is a `Result`. So you'll need to use `map` I presume. I personally like the style that `try!` encourages.