pub enum Result<T, E> {
Ok(T),
Err(E),
}Expand description
Result is a type that represents either success (Ok) or failure (Err).
See the module documentation for details.
Variants§
Implementations§
Source§impl<T, E> Result<T, E>
impl<T, E> Result<T, E>
1.70.0 (const: unstable) · Sourcepub fn is_ok_and<F>(self, f: F) -> bool
pub fn is_ok_and<F>(self, f: F) -> bool
Returns true if the result is Ok and the value inside of it matches a predicate.
§Examples
let x: Result<u32, &str> = Ok(2);
assert_eq!(x.is_ok_and(|x| x > 1), true);
let x: Result<u32, &str> = Ok(0);
assert_eq!(x.is_ok_and(|x| x > 1), false);
let x: Result<u32, &str> = Err("hey");
assert_eq!(x.is_ok_and(|x| x > 1), false);
let x: Result<String, &str> = Ok("ownership".to_string());
assert_eq!(x.as_ref().is_ok_and(|x| x.len() > 1), true);
println!("still alive {:?}", x);1.70.0 (const: unstable) · Sourcepub fn is_err_and<F>(self, f: F) -> bool
pub fn is_err_and<F>(self, f: F) -> bool
Returns true if the result is Err and the value inside of it matches a predicate.
§Examples
use std::io::{Error, ErrorKind};
let x: Result<u32, Error> = Err(Error::new(ErrorKind::NotFound, "!"));
assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), true);
let x: Result<u32, Error> = Err(Error::new(ErrorKind::PermissionDenied, "!"));
assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), false);
let x: Result<u32, Error> = Ok(123);
assert_eq!(x.is_err_and(|x| x.kind() == ErrorKind::NotFound), false);
let x: Result<u32, String> = Err("ownership".to_string());
assert_eq!(x.as_ref().is_err_and(|x| x.len() > 1), true);
println!("still alive {:?}", x);1.0.0 (const: 1.48.0) · Sourcepub const fn as_ref(&self) -> Result<&T, &E>
pub const fn as_ref(&self) -> Result<&T, &E>
Converts from &Result<T, E> to Result<&T, &E>.
Produces a new Result, containing a reference
into the original, leaving the original in place.
§Examples
1.0.0 (const: 1.83.0) · Sourcepub const fn as_mut(&mut self) -> Result<&mut T, &mut E>
pub const fn as_mut(&mut self) -> Result<&mut T, &mut E>
Converts from &mut Result<T, E> to Result<&mut T, &mut E>.
§Examples
1.0.0 (const: unstable) · Sourcepub fn map<U, F>(self, op: F) -> Result<U, E>where
F: FnOnce(T) -> U,
pub fn map<U, F>(self, op: F) -> Result<U, E>where
F: FnOnce(T) -> U,
1.41.0 (const: unstable) · Sourcepub fn map_or<U, F>(self, default: U, f: F) -> Uwhere
F: FnOnce(T) -> U,
T:,
E:,
U:,
pub fn map_or<U, F>(self, default: U, f: F) -> Uwhere
F: FnOnce(T) -> U,
T:,
E:,
U:,
Returns the provided default (if Err), or
applies a function to the contained value (if Ok).
Arguments passed to map_or are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use map_or_else,
which is lazily evaluated.
§Examples
1.41.0 (const: unstable) · Sourcepub fn map_or_else<U, D, F>(self, default: D, f: F) -> U
pub fn map_or_else<U, D, F>(self, default: D, f: F) -> U
Sourcepub const fn map_or_default<U, F>(self, f: F) -> U
🔬This is a nightly-only experimental API. (result_option_map_or_default #138099)
pub const fn map_or_default<U, F>(self, f: F) -> U
result_option_map_or_default #138099)Maps a Result<T, E> to a U by applying function f to the contained
value if the result is Ok, otherwise if Err, returns the
default value for the type U.
§Examples
1.0.0 (const: unstable) · Sourcepub fn map_err<F, O>(self, op: O) -> Result<T, F>where
O: FnOnce(E) -> F,
pub fn map_err<F, O>(self, op: O) -> Result<T, F>where
O: FnOnce(E) -> F,
1.76.0 (const: unstable) · Sourcepub fn inspect_err<F>(self, f: F) -> Self
pub fn inspect_err<F>(self, f: F) -> Self
1.47.0 (const: unstable) · Sourcepub fn as_deref(&self) -> Result<&T::Target, &E>where
T: Deref,
pub fn as_deref(&self) -> Result<&T::Target, &E>where
T: Deref,
1.47.0 (const: unstable) · Sourcepub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E>where
T: DerefMut,
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E>where
T: DerefMut,
Converts from Result<T, E> (or &mut Result<T, E>) to Result<&mut <T as DerefMut>::Target, &mut E>.
Coerces the Ok variant of the original Result via DerefMut
and returns the new Result.
§Examples
let mut s = "HELLO".to_string();
let mut x: Result<String, u32> = Ok("hello".to_string());
let y: Result<&mut str, &mut u32> = Ok(&mut s);
assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
let mut i = 42;
let mut x: Result<String, u32> = Err(42);
let y: Result<&mut str, &mut u32> = Err(&mut i);
assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);1.16.0 (const: unstable) · Sourcepub fn unwrap_or_default(self) -> Twhere
T: Default,
E:,
pub fn unwrap_or_default(self) -> Twhere
T: Default,
E:,
Returns the contained Ok value or a default
Consumes the self argument then, if Ok, returns the contained
value, otherwise if Err, returns the default value for that
type.
§Examples
Converts a string to an integer, turning poorly-formed strings
into 0 (the default value for integers). parse converts
a string to any other type that implements FromStr, returning an
Err on error.
1.0.0 (const: unstable) · Sourcepub fn and<U>(self, res: Result<U, E>) -> Result<U, E>where
T:,
E:,
U:,
pub fn and<U>(self, res: Result<U, E>) -> Result<U, E>where
T:,
E:,
U:,
Returns res if the result is Ok, otherwise returns the Err value of self.
Arguments passed to and are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use and_then, which is
lazily evaluated.
§Examples
let x: Result<u32, &str> = Ok(2);
let y: Result<&str, &str> = Err("late error");
assert_eq!(x.and(y), Err("late error"));
let x: Result<u32, &str> = Err("early error");
let y: Result<&str, &str> = Ok("foo");
assert_eq!(x.and(y), Err("early error"));
let x: Result<u32, &str> = Err("not a 2");
let y: Result<&str, &str> = Err("late error");
assert_eq!(x.and(y), Err("not a 2"));
let x: Result<u32, &str> = Ok(2);
let y: Result<&str, &str> = Ok("different result type");
assert_eq!(x.and(y), Ok("different result type"));1.0.0 (const: unstable) · Sourcepub fn and_then<U, F>(self, op: F) -> Result<U, E>
pub fn and_then<U, F>(self, op: F) -> Result<U, E>
Calls op if the result is Ok, otherwise returns the Err value of self.
This function can be used for control flow based on Result values.
§Examples
fn sq_then_to_string(x: u32) -> Result<String, &'static str> {
x.checked_mul(x).map(|sq| sq.to_string()).ok_or("overflowed")
}
assert_eq!(Ok(2).and_then(sq_then_to_string), Ok(4.to_string()));
assert_eq!(Ok(1_000_000).and_then(sq_then_to_string), Err("overflowed"));
assert_eq!(Err("not a number").and_then(sq_then_to_string), Err("not a number"));Often used to chain fallible operations that may return Err.
use std::{io::ErrorKind, path::Path};
// Note: on Windows "/" maps to "C:\"
let root_modified_time = Path::new("/").metadata().and_then(|md| md.modified());
assert!(root_modified_time.is_ok());
let should_fail = Path::new("/bad/path").metadata().and_then(|md| md.modified());
assert!(should_fail.is_err());
assert_eq!(should_fail.unwrap_err().kind(), ErrorKind::NotFound);1.0.0 (const: unstable) · Sourcepub fn or<F>(self, res: Result<T, F>) -> Result<T, F>where
T:,
E:,
F:,
pub fn or<F>(self, res: Result<T, F>) -> Result<T, F>where
T:,
E:,
F:,
Returns res if the result is Err, otherwise returns the Ok value of self.
Arguments passed to or are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use or_else, which is
lazily evaluated.
§Examples
let x: Result<u32, &str> = Ok(2);
let y: Result<u32, &str> = Err("late error");
assert_eq!(x.or(y), Ok(2));
let x: Result<u32, &str> = Err("early error");
let y: Result<u32, &str> = Ok(2);
assert_eq!(x.or(y), Ok(2));
let x: Result<u32, &str> = Err("not a 2");
let y: Result<u32, &str> = Err("late error");
assert_eq!(x.or(y), Err("late error"));
let x: Result<u32, &str> = Ok(2);
let y: Result<u32, &str> = Ok(100);
assert_eq!(x.or(y), Ok(2));1.0.0 (const: unstable) · Sourcepub fn or_else<F, O>(self, op: O) -> Result<T, F>
pub fn or_else<F, O>(self, op: O) -> Result<T, F>
Calls op if the result is Err, otherwise returns the Ok value of self.
This function can be used for control flow based on result values.
§Examples
fn sq(x: u32) -> Result<u32, u32> { Ok(x * x) }
fn err(x: u32) -> Result<u32, u32> { Err(x) }
assert_eq!(Ok(2).or_else(sq).or_else(sq), Ok(2));
assert_eq!(Ok(2).or_else(err).or_else(sq), Ok(2));
assert_eq!(Err(3).or_else(sq).or_else(err), Ok(9));
assert_eq!(Err(3).or_else(err).or_else(err), Err(3));1.0.0 (const: unstable) · Sourcepub fn unwrap_or(self, default: T) -> Twhere
T:,
E:,
pub fn unwrap_or(self, default: T) -> Twhere
T:,
E:,
Returns the contained Ok value or a provided default.
Arguments passed to unwrap_or are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use unwrap_or_else,
which is lazily evaluated.
§Examples
1.0.0 (const: unstable) · Sourcepub fn unwrap_or_else<F>(self, op: F) -> Twhere
F: FnOnce(E) -> T,
pub fn unwrap_or_else<F>(self, op: F) -> Twhere
F: FnOnce(E) -> T,
1.58.0 · Sourcepub unsafe fn unwrap_unchecked(self) -> T
pub unsafe fn unwrap_unchecked(self) -> T
1.58.0 · Sourcepub unsafe fn unwrap_err_unchecked(self) -> E
pub unsafe fn unwrap_err_unchecked(self) -> E
Source§impl<T, E> Result<&T, E>
impl<T, E> Result<&T, E>
Source§impl<T, E> Result<&mut T, E>
impl<T, E> Result<&mut T, E>
1.59.0 (const: 1.83.0) · Sourcepub const fn copied(self) -> Result<T, E>where
T: Copy,
pub const fn copied(self) -> Result<T, E>where
T: Copy,
Maps a Result<&mut T, E> to a Result<T, E> by copying the contents of the
Ok part.
§Examples
Trait Implementations§
Source§impl<T, E, F: From<E>> FromResidual<Result<Infallible, E>> for Result<T, F>
impl<T, E, F: From<E>> FromResidual<Result<Infallible, E>> for Result<T, F>
Source§fn from_residual(residual: Result<Infallible, E>) -> Self
fn from_residual(residual: Result<Infallible, E>) -> Self
try_trait_v2 #84277)Residual type. Read moreSource§impl<T, E> Residual<T> for Result<Infallible, E>
impl<T, E> Residual<T> for Result<Infallible, E>
Source§impl<T, E> Try for Result<T, E>
impl<T, E> Try for Result<T, E>
Source§type Output = T
type Output = T
try_trait_v2 #84277)? when not short-circuiting.Source§type Residual = Result<Infallible, E>
type Residual = Result<Infallible, E>
try_trait_v2 #84277)FromResidual::from_residual
as part of ? when short-circuiting. Read moreSource§fn from_output(output: Self::Output) -> Self
fn from_output(output: Self::Output) -> Self
try_trait_v2 #84277)Output type. Read moreSource§fn branch(self) -> ControlFlow<Self::Residual, Self::Output>
fn branch(self) -> ControlFlow<Self::Residual, Self::Output>
try_trait_v2 #84277)? to decide whether the operator should produce a value
(because this returned ControlFlow::Continue)
or propagate a value back to the caller
(because this returned ControlFlow::Break). Read more