Trait TryExtract
Source pub trait TryExtract<'a> {
type Ok;
type Err;
// Required method
fn try_extract(self) -> Result<Self::Ok, Self::Err>;
}
Available on crate feature try only.
Expand description
A trait for uniformly extracting success/error values from various container
types.
This provides a generic interface to convert different outcome-carrying
types into a standard Result form, enabling unified error handling
across:
The conversion preserves semantic meanings:
- Success cases map to
Ok
- Termination/error cases map to
Err
The type of success values (may be a reference)
The type of error values (may be a reference)
Converts the container into a Result, preserving semantic meaning
For reference implementations (&Result, &Option, &ControlFlow),
this returns references to contained values rather than moving them
Extracts references from ControlFlow:
- Continue(c) => Ok(&c)
- Break(b) => Err(&b)
Extracts reference to the contained value if exists
- &Option:
- Some(value) => Ok(&value)
- None => Err(())
Extracts references to the contained value:
- &Result<Ok, Err> => Result<&Ok, &Err>
Directly returns the owned Result (identity conversion)
Converts ControlFlow into Result:
- Continue(c) => Ok(c)
- Break(b) => Err(b)
Converts an Option into a Result:
- Some(value) => Ok(value)
- None => Err(())
This consumes the Option and moves the contained value