pub trait TakeSome {
type Item;
// Required method
fn take_some(&mut self) -> Option<Self::Item>;
// Provided method
fn split_at_some(self) -> Result<(Self::Item, Self), Self>
where Self: Sized { ... }
}
Expand description
A helper trait that allows to take some value out of a collection, i.e. remove a single element and return it, while preserving all others.
Required Associated Types§
Required Methods§
Sourcefn take_some(&mut self) -> Option<Self::Item>
fn take_some(&mut self) -> Option<Self::Item>
Takes “some” element from a collection while preserving all others.
In general case it could not be defined which exactly element will be returned (first, last, greatest, least, …), so, I repeat, in general case it is better to make no assumptions on the ordering or anything else.
There are several assumptions that trait implementors should take care of:
None
should be returned if and only if the collection is empty.- If the collection is NOT empty, exactly ONE element should be taken out from it, i.e. all the rest items should remains there.
- In case the collection IS empty, this call should NOT modify the collection.
- The implementors are encouraged to implement the method in a way that it will cause the least overhead possible.
Provided Methods§
Sourcefn split_at_some(self) -> Result<(Self::Item, Self), Self>where
Self: Sized,
fn split_at_some(self) -> Result<(Self::Item, Self), Self>where
Self: Sized,
Splits a given collection at “some” element, and returns a pair of that element and the remaining collection, if “some” element was found, or returns the original collection if “some” element could not be found. The latter happens if the collection is empty.