Error Handling
Result
Section titled “Result”enum Result<T, E> { Ok(T), Error(E),}Result represents recoverable failures that callers must handle explicitly.
? Operator
Section titled “? Operator”? propagates an error to the caller if the value is Error.
This is equivalent to early return on Error.
Decisions
Section titled “Decisions”- Recoverable errors use
Result<T, E>. - Unrecoverable errors use
panic. ?is the standard propagation operator forResult.?is not defined forOption<T>in v0.1.
panic aborts execution when the program is in an invalid state.
Example:
i32 index(i32[] values, i32 i) { if i < 0 || i >= values.len() { panic("index out of range"); } return values[i];}Example:
Result<i32, string> parse_port(string s) { let p = int.parse(s)?; if p < 1 || p > 65535 { return Error("port out of range"); } return Ok(p);}Example
Section titled “Example”Option<i32> maybe_port(string s) { // explicit matching; `?` is only for Result in v0.1 match int.parse(s) { Ok(v) => Some(v), Error(_) => None, }}Canonical stdlib API contract for Result is documented in docs/standard-library/Core/Results.md.