diesel::prelude

Trait OptionalExtension

Source
pub trait OptionalExtension<T> {
    // Required method
    fn optional(self) -> Result<Option<T>, Error>;
}
Expand description

Required Methods§

Source

fn optional(self) -> Result<Option<T>, Error>

Converts a QueryResult<T> into a QueryResult<Option<T>>.

By default, Diesel treats 0 rows being returned from a query that is expected to return 1 row as an error (e.g. the return value of get_result or first). This method will handle that error, and give you back an Option<T> instead.

§Example
use diesel::{QueryResult, NotFound, OptionalExtension};

let result: QueryResult<i32> = Ok(1);
assert_eq!(Ok(Some(1)), result.optional());

let result: QueryResult<i32> = Err(NotFound);
assert_eq!(Ok(None), result.optional());

Implementors§