another-option
This package provides Opt<T>
as an alternative to Option<T>
. Why would you want another option? Opt
provides advantages when:
- the generic type,
T
, is expensive to allocate, and - mutation between
None
andSome(...)
is frequent.
Examples
Since Rust's built-in Option<T>
is an enum, it will drop its Some(...)
value when None
is assigned.
let mut option: = Some;
option = None; // drops the string
option = Some; // allocation
Since Opt<T>
always owns the value, even when empty, the value can be reused without drops or allocations:
use crate Opt;
let mut opt: = some;
opt.map_in_place;
opt.set_none; // does *not* drop the string
opt.set_some; // does *not* drop the string
assert_eq!;