Introducing ?
Sometimes we just want the simplicity of unwrap without the possibility of
a panic. Until now, unwrap has forced us to nest deeper and deeper when
what we really wanted was to get the variable out. This is exactly the purpose of ?.
Upon finding an Err, there are two valid actions to take:
panic!which we already decided to try to avoid if possiblereturnbecause anErrmeans it cannot be handled
? is almost[^†] exactly equivalent to an unwrap which returns
instead of panicking on Errs. Let's see how we can simplify the earlier
example that used combinators:
#[derive(Drop)]
struct ParseError {
message: ByteArray,
}
// Helper function to parse a single ASCII digit from a ByteArray
fn parse_ascii_digit(value: ByteArray) -> Result<u32, ParseError> {
if value.len() != 1 {
Err(ParseError { message: "Expected a single character" })
} else {
let byte = value[0];
if byte >= '0' && byte <= '9' {
Ok((byte - '0').into())
} else {
Err(ParseError { message: "Character is not a digit" })
}
}
}
fn multiply(first_number: ByteArray, second_number: ByteArray) -> Result<u32, ParseError> {
let first_number = parse_ascii_digit(first_number)?;
let second_number = parse_ascii_digit(second_number)?;
Ok(first_number * second_number)
}
fn print(result: Result<u32, ParseError>) {
match result {
Ok(n) => println!("n is {}", n),
Err(e) => println!("Error: {}", e.message),
}
}
fn main() {
print(multiply("4", "5"));
print(multiply("t", "2"));
}