10.9 Enums as the Basis for Option
and Result
Rust's standard library uses enums extensively, particularly for the Option
and Result
types.
10.9.1 The Option
Enum
#![allow(unused)] fn main() { enum Option<T> { Some(T), None, } }
- Usage: Represents an optional value.
- Pattern Matching: Used to safely handle cases where a value may be absent.
10.9.2 The Result
Enum
#![allow(unused)] fn main() { enum Result<T, E> { Ok(T), Err(E), } }
- Usage: Used for error handling.
- Pattern Matching: Allows handling success and error cases explicitly.
Note: We will explore Option
, Result
, and error handling in detail in later chapters.