Is there any way of doing unwrap_or_return an Error (any error)

℡╲_俬逩灬. 提交于 2020-07-10 06:57:18

问题


Is there any way to simplify the returns in the following example (originally copied from here):

use std::num::ParseIntError;

fn multiply(first_number_str: &str, second_number_str: &str) -> Result<i32, ParseIntError> {
    let first_number = match first_number_str.parse::<i32>() {
        Ok(first_number)  => first_number,
        Err(e) => return Err(e),
    };

    let second_number = match second_number_str.parse::<i32>() {
        Ok(second_number)  => second_number,
        Err(e) => return Err(AnotherError::ParseError("error")),
    };

    Ok(first_number * second_number)
}

I mean something like:

use std::num::ParseIntError;

fn multiply(first_number_str: &str, second_number_str: &str) -> Result<i32, ParseIntError> {
    let first_number = first_number_str.parse::<i32>()
        .unwrap_or_return(|e| Err(e));

    let second_number = second_number_str.parse::<i32>()
        .unwrap_or_return(|e| Err(AnotherError::ParseError("error"));

    Ok(first_number * second_number)
}

回答1:


You're looking for the question mark operator, possibly combined with Result::or or Result::or_else, depending on the specifics of your use-case.

That code example can be rewritten as

use std::num::ParseIntError;

pub fn multiply(first_number_str: &str, second_number_str: &str) -> Result<i32, ParseIntError> {
    let first_number = first_number_str.parse::<i32>()?;
    let second_number = second_number_str.parse::<i32>().or_else(|e| Err(e))?;
    // The closure in `or_else` could also return `Ok` or some different error with type `ParseIntError`.
    // let second_number = second_number_str.parse::<i32>().or_else(|_e| Ok(27))?;

    Ok(first_number * second_number)
}

(playground)

If you know that you're going to return Ok in the or_else, Result::unwrap_or is more appropriate. Check out the other similar methods on Result to see what's offered.



来源:https://stackoverflow.com/questions/60011028/is-there-any-way-of-doing-unwrap-or-return-an-error-any-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!