On this page
Result
Prev: Option | Up: builtins | Next: Vec
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — Result
Tags: #public #prog #rust #builtins #tier/core
Availability
Built-in / Prelude (Prelude Enum) — available with zero use imports.
Syntax & Overview
enum Result<T, E> { Ok(T), Err(E) }
Explicit recoverable error handling without exceptions; propagates cleanly via the ? operator.
Notes & Pitfalls
- Explicit recoverable errors:
Ok(T)orErr(E)—marked#[must_use], so ignoring aResulttriggers a compiler warning. - Propagate cleanly with
?: Automatically converts error types viaFromwhen returningResultfrom a function.
Example
fn parse_port(raw: &str) -> Result<u16, std::num::ParseIntError> {
let port: u16 = raw.trim().parse()?;
Ok(port)
}
fn main() {
assert_eq!(parse_port(" 8080 "), Ok(8080));
assert!(parse_port("abc").is_err());
}
Linked from
Related notes
- tuts/rust/builtins/Option Linked here; links to this page; shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Vec Linked here; links to this page; shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins Linked here; links to this page; shared tags: #builtins, #prog, #rust
- tuts/rust/builtins/try_macro links to this page; shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/ownership_and_internals/enums_pattern_matching_and_errors links to this page; shared tags: #prog, #rust
- tuts/rust/ownership_and_internals/ownership_borrowing_and_lifetimes Linked here; shared tags: #prog, #rust
- tuts/rust/tutorials/cli_file_processor_with_result links to this page; shared tags: #prog, #rust
- tuts/rust/builtins/Clone shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Copy shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Drop shared tags: #builtins, #prog, #rust, #tier