pub enum Nullable<T> {
Null,
Present(T),
}
The Nullable type. Represents a value which may be specified as null on an API.
Note that this is distinct from a value that is optional and not present!
Nullable implements many of the same methods as the Option type (map, unwrap, etc).
Returns true
if the Nullable is a Present
value.
let x: Nullable<u32> = Nullable::Present(2);
assert_eq!(x.is_present(), true);
let x: Nullable<u32> = Nullable::Null;
assert_eq!(x.is_present(), false);
Returns true
if the Nullable is a Null
value.
let x: Nullable<u32> = Nullable::Present(2);
assert_eq!(x.is_null(), false);
let x: Nullable<u32> = Nullable::Null;
assert_eq!(x.is_null(), true);
Converts from Nullable<T>
to Nullable<&T>
.
Convert an Nullable<
String
>
into a Nullable<
usize
>
, preserving the original.
The map
method takes the self
argument by value, consuming the original,
so this technique uses as_ref
to first take a Nullable
to a reference
to the value inside the original.
let num_as_str: Nullable<String> = Nullable::Present("10".to_string());
let num_as_int: Nullable<usize> = num_as_str.as_ref().map(|n| n.len());
println!("still can print num_as_str: {:?}", num_as_str);
Converts from Nullable<T>
to Nullable<&mut T>
.
let mut x = Nullable::Present(2);
match x.as_mut() {
Nullable::Present(v) => *v = 42,
Nullable::Null => {},
}
assert_eq!(x, Nullable::Present(42));
Unwraps a Nullable, yielding the content of a Nullable::Present
.
Panics if the value is a Nullable::Null
with a custom panic message provided by
msg
.
let x = Nullable::Present("value");
assert_eq!(x.expect("the world is ending"), "value");
let x: Nullable<&str> = Nullable::Null;
x.expect("the world is ending");
Moves the value v
out of the Nullable<T>
if it is Nullable::Present(v)
.
In general, because this function may panic, its use is discouraged.
Instead, prefer to use pattern matching and handle the Nullable::Null
case explicitly.
Panics if the self value equals Nullable::Null
.
let x = Nullable::Present("air");
assert_eq!(x.unwrap(), "air");
let x: Nullable<&str> = Nullable::Null;
assert_eq!(x.unwrap(), "air");
Returns the contained value or a default.
assert_eq!(Nullable::Present("car").unwrap_or("bike"), "car");
assert_eq!(Nullable::Null.unwrap_or("bike"), "bike");
Returns the contained value or computes it from a closure.
let k = 10;
assert_eq!(Nullable::Present(4).unwrap_or_else(|| 2 * k), 4);
assert_eq!(Nullable::Null.unwrap_or_else(|| 2 * k), 20);
Maps a Nullable<T>
to Nullable<U>
by applying a function to a contained value.
Convert a Nullable<
String
>
into a Nullable<
usize
>
, consuming the original:
let maybe_some_string = Nullable::Present(String::from("Hello, World!"));
let maybe_some_len = maybe_some_string.map(|s| s.len());
assert_eq!(maybe_some_len, Nullable::Present(13));
Applies a function to the contained value (if any),
or returns a default
(if not).
let x = Nullable::Present("foo");
assert_eq!(x.map_or(42, |v| v.len()), 3);
let x: Nullable<&str> = Nullable::Null;
assert_eq!(x.map_or(42, |v| v.len()), 42);
Applies a function to the contained value (if any),
or computes a default
(if not).
let k = 21;
let x = Nullable::Present("foo");
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 3);
let x: Nullable<&str> = Nullable::Null;
assert_eq!(x.map_or_else(|| 2 * k, |v| v.len()), 42);
Transforms the Nullable<T>
into a Result<T, E>
, mapping Nullable::Present(v)
to
Ok(v)
and Nullable::Null
to Err(err)
.
let x = Nullable::Present("foo");
assert_eq!(x.ok_or(0), Ok("foo"));
let x: Nullable<&str> = Nullable::Null;
assert_eq!(x.ok_or(0), Err(0));
Transforms the Nullable<T>
into a Result<T, E>
, mapping Nullable::Present(v)
to
Ok(v)
and Nullable::Null
to Err(err())
.
let x = Nullable::Present("foo");
assert_eq!(x.ok_or_else(|| 0), Ok("foo"));
let x: Nullable<&str> = Nullable::Null;
assert_eq!(x.ok_or_else(|| 0), Err(0));
Returns Nullable::Null
if the Nullable is Nullable::Null
, otherwise returns optb
.
let x = Nullable::Present(2);
let y: Nullable<&str> = Nullable::Null;
assert_eq!(x.and(y), Nullable::Null);
let x: Nullable<u32> = Nullable::Null;
let y = Nullable::Present("foo");
assert_eq!(x.and(y), Nullable::Null);
let x = Nullable::Present(2);
let y = Nullable::Present("foo");
assert_eq!(x.and(y), Nullable::Present("foo"));
let x: Nullable<u32> = Nullable::Null;
let y: Nullable<&str> = Nullable::Null;
assert_eq!(x.and(y), Nullable::Null);
Returns Nullable::Null
if the Nullable is Nullable::Null
, otherwise calls f
with the
wrapped value and returns the result.
Some languages call this operation flatmap.
fn sq(x: u32) -> Nullable<u32> { Nullable::Present(x * x) }
fn nope(_: u32) -> Nullable<u32> { Nullable::Null }
assert_eq!(Nullable::Present(2).and_then(sq).and_then(sq), Nullable::Present(16));
assert_eq!(Nullable::Present(2).and_then(sq).and_then(nope), Nullable::Null);
assert_eq!(Nullable::Present(2).and_then(nope).and_then(sq), Nullable::Null);
assert_eq!(Nullable::Null.and_then(sq).and_then(sq), Nullable::Null);
Returns the Nullable if it contains a value, otherwise returns optb
.
let x = Nullable::Present(2);
let y = Nullable::Null;
assert_eq!(x.or(y), Nullable::Present(2));
let x = Nullable::Null;
let y = Nullable::Present(100);
assert_eq!(x.or(y), Nullable::Present(100));
let x = Nullable::Present(2);
let y = Nullable::Present(100);
assert_eq!(x.or(y), Nullable::Present(2));
let x: Nullable<u32> = Nullable::Null;
let y = Nullable::Null;
assert_eq!(x.or(y), Nullable::Null);
Returns the Nullable if it contains a value, otherwise calls f
and
returns the result.
fn nobody() -> Nullable<&'static str> { Nullable::Null }
fn vikings() -> Nullable<&'static str> { Nullable::Present("vikings") }
assert_eq!(Nullable::Present("barbarians").or_else(vikings),
Nullable::Present("barbarians"));
assert_eq!(Nullable::Null.or_else(vikings), Nullable::Present("vikings"));
assert_eq!(Nullable::Null.or_else(nobody), Nullable::Null);
Takes the value out of the Nullable, leaving a Nullable::Null
in its place.
let mut x = Nullable::Present(2);
x.take();
assert_eq!(x, Nullable::Null);
let mut x: Nullable<u32> = Nullable::Null;
x.take();
assert_eq!(x, Nullable::Null);
Maps an Nullable<&T>
to an Nullable<T>
by cloning the contents of the
Nullable.
let x = 12;
let opt_x = Nullable::Present(&x);
assert_eq!(opt_x, Nullable::Present(&12));
let cloned = opt_x.cloned();
assert_eq!(cloned, Nullable::Present(12));
Returns the contained value or a default
Consumes the self
argument then, if Nullable::Present
, returns the contained
value, otherwise if Nullable::Null
, returns the default value for that
type.
let x = Nullable::Present(42);
assert_eq!(42, x.unwrap_or_default());
let y: Nullable<i32> = Nullable::Null;
assert_eq!(0, y.unwrap_or_default());
Performs copy-assignment from source
. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Formats the value using the given formatter. Read more
Serialize this value into the given Serde serializer. Read more
Deserialize this value from the given Serde deserializer. Read more
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static