Struct std::prelude::Result
Either a success value or an error value.
See module level documentation for more information.
Example
let ok: Result<i32, i32> = Result::ok(1);
let err: Result<i32, i32> = Result::err(-1);
assert!(ok.is_ok());
assert!(err.is_err());
assert_eq!(ok.unwrap(), 1);
assert_eq!(err.unwrap_err(), -1);
Run this example
Methods
impl Result<T, E> { ... }
-
fn ok(ok: T) -> Result<T, E>
Create a success variant
Example
let r: Result<i32, i32> = Result::ok(10); assert!(r.is_ok()); assert_eq!(r.unwrap(), 10);
Run this example -
fn err(err: E) -> Result<T, E>
Create a error variant
Example
let r: Result<i32, i32> = Result::err(42); assert!(r.is_err()); assert_eq!(r.unwrap_err(), 42);
Run this example -
Returns
true
if the result conains an OK variant,false
otherwise -
Returns
true
if the result conains an error variant,false
otherwise -
Converts from
Result<T, E>
toOption<T>
.If the result contains an error, it returns
Option::none()
.Example
let r1: Result<i32, i32> = Result::ok(10); let r2: Result<i32, i32> = Result::err(20); let o1: Option<i32> = r1.get(); let o2: Option<i32> = r2.get(); assert!(o1.is_some()); assert_eq!(o1.unwrap(), 10); assert!(o2.is_none());
Run this example -
Converts from
Result<T, E>
toOption<E>
.If the result contains an OK varianr, it returns
Option::none()
.Example
let r1: Result<i32, i32> = Result::ok(10); let r2: Result<i32, i32> = Result::err(20); let o1: Option<i32> = r1.get_err(); let o2: Option<i32> = r2.get_err(); assert!(o1.is_none()); assert!(o2.is_some()); assert_eq!(o2.unwrap(), 20);
Run this example -
fn unwrap(self: Result<T, E>) -> T
Return the success value, panicking if error value is present
Examples
let r: Result<i32, i32> = Result::ok(10); assert_eq!(r.unwrap(), 10);
Run this examplelet r: Result<i32, i32> = Result::err(42); r.unwrap(); // panics
Run this example -
fn unwrap_err(self: Result<T, E>) -> E
Return the error value, panicking if success value is present
Examples
let r: Result<i32, i32> = Result::err(10); assert_eq!(r.unwrap_err(), 10);
Run this examplelet r: Result<i32, i32> = Result::ok(42); r.unwrap_err(); // panics
Run this example -
fn unwrap_or(self: Result<T, E>, val: T) -> T
Return the success value or a default value
Example
let r1: Result<i32, i32> = Result::ok(10); let r2: Result<i32, i32> = Result::err(20); assert_eq!(r1.unwrap_or(30), 10); assert_eq!(r2.unwrap_or(40), 40);
Run this example -
fn unwrap_or_else<F>(self: Result<T, E>, func: F) -> T
F: Fn(E) -> TReturn the success value or a default value provided by a callback
Example
let r1: Result<i32, i32> = Result::ok(10); let r2: Result<i32, i32> = Result::err(20); assert_eq!(r1.unwrap_or_else(|_: i32| -> i32 { i32::max_value() }), 10); assert_eq!(r2.unwrap_or_else(|_: i32| -> i32 { i32::max_value() }), i32::max_value());
Run this example -
Transform the success value inside the
Result
by calling the provided function.If the error value is present, function is not called and the original error is passed through.
Example
let a: Result<i32, ()> = Result::ok(42); let b: Result<i32, ()> = Result::err(()); assert_eq!(a.map(|v: i32| -> i32 { v + 1 }), Result::ok(43)); assert_eq!(b.map(|v: i32| -> i32 { v + 1 }), Result::err(()));
Run this example -
Transform the error value inside the
Result
by calling the provided function.If the success value is present, function is not called and the original error is passed through.
Example
let a: Result<(), i32> = Result::err(42); let b: Result<(), i32> = Result::ok(()); assert_eq!(a.map_err(|v: i32| -> i32 { v + 1 }), Result::err(43)); assert_eq!(b.map_err(|v: i32| -> i32 { v + 1 }), Result::ok(()));
Run this example -
Transform the value inside the
Result
by calling the provided function, which returns aResult
.If the success value is present, function is not called and the original error is passed through.
impl Result { ... }
-
Convert
Result<Option<T>, E>
toOption<Result<T, E>>
See also transpose for the reverse operation.
Example
let a: Result<Option<i32>, i32> = Result::ok(Option::some(42)); let b: Result<Option<i32>, i32> = Result::ok(Option::none()); let c: Result<Option<i32>, i32> = Result::err(1337); assert_eq!(a.transpose(), Option::some(Result::ok(42))); assert_eq!(b.transpose(), Option::none()); assert_eq!(c.transpose(), Option::some(Result::err(1337)));
Run this example -
fn fmt<T, E, F>(self: &Result<T, E>, formatter: &mut F) -> Result
T: Formattable<T, F>E: Formattable<E, F>F: Formatter<F> -
Frees the memory backing the object.
Protocols
Macros
Mixins
impl Result { ... }
-
-
Returns
false
if arguments are equal,true
otherwise