我有一个函数调用另一个返回a的函数Result。我需要检查Resultis是Ok或Err,如果它是an Err,则需要return从我的函数开始。这就是我现在正在做的:
match callable(&mut param) {
Ok(_v) => (),
Err(_e) => return,
};
Run Code Online (Sandbox Code Playgroud)
有没有更惯用的Rust方法来做到这一点?
是否有类似于?快捷方式的东西,它在出现错误时不是从函数返回结果,而是返回预定义的值?
基本上我想知道是否可以在一行中执行以下操作:
fn index() -> String {
let temp = some_func("pass"); // some_func returns a Result
if temp.is_err() {
return "No parameters named pass".to_string();
}
try_decrypt_data(temp.unwrap())
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用unwrap_or_else(),但这只是返回闭包而不是外部函数。IE
try_decrypt_data(params.get("pass").unwrap_or_else(|| return "No parameters named pass".to_string(); )) // This doesn't work
Run Code Online (Sandbox Code Playgroud)