On this page
  1. Result
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

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

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