let else
Statements
如需了解匹配模式并从函数返回的常见情况,请使用 let else
。“else” 分支必须执行不同的结束方式(例如,return
、break
或 panic
,但不能直接执行到代码块的末尾)。
fn hex_or_die_trying(maybe_string: Option<String>) -> Result<u32, String> { let s = if let Some(s) = maybe_string { s } else { return Err(String::from("got None")); }; let first_byte_char = if let Some(first) = s.chars().next() { first } else { return Err(String::from("got empty string")); }; let digit = if let Some(digit) = first_byte_char.to_digit(16) { digit } else { return Err(String::from("not a hex digit")); }; Ok(digit) } fn main() { println!("result: {:?}", hex_or_die_trying(Some(String::from("foo")))); }
The rewritten version is:
#![allow(unused)] fn main() { fn hex_or_die_trying(maybe_string: Option<String>) -> Result<u32, String> { let Some(s) = maybe_string else { return Err(String::from("got None")); }; let Some(first_byte_char) = s.chars().next() else { return Err(String::from("got empty string")); }; let Some(digit) = first_byte_char.to_digit(16) else { return Err(String::from("not a hex digit")); }; Ok(digit) } }
探索更多
- This early return-based control flow is common in Rust error handling code, where you try to get a value out of a
Result
, returning an error if theResult
wasErr
. - If students ask, you can also demonstrate how real error handling code would be written with
?
.