有没有办法做 unwrap_or_return 一个错误(任何错误)
Is there any way of doing unwrap_or_return an Error (any error)
有什么方法可以简化以下示例中的 returns(最初是从 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)
}
我的意思是:
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)
}
您正在寻找 question mark operator, possibly combined with Result::or
or Result::or_else
,具体取决于您的用例的具体情况。
该代码示例可以重写为
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)
}
如果你知道你要在or_else
returnOk
,Result::unwrap_or
是比较合适的。查看 Result
上的其他类似方法以查看提供的内容。
有什么方法可以简化以下示例中的 returns(最初是从 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)
}
我的意思是:
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)
}
您正在寻找 question mark operator, possibly combined with Result::or
or Result::or_else
,具体取决于您的用例的具体情况。
该代码示例可以重写为
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)
}
如果你知道你要在or_else
returnOk
,Result::unwrap_or
是比较合适的。查看 Result
上的其他类似方法以查看提供的内容。