This crate exposes a Single
trait for extracting the element from a single-element iterator.
You may use this crate under the MIT license or the Apache License (Version 2) at your discretion. This crate is dual-licensed for compatibility with rust itself.
rust
pub trait Single {
type Item;
fn single(self) -> Result<Self::Item, Error>;
}
type Item
The item type of the wrapped iterator.
fn single(self) -> Result<Self::Item, Error>
Get the single element from a single-element iterator.
Note that many iterators return references to the elements, so this method will as well if the backing iterator does.
assert_eq!(iter::empty::().single(), Err(single::Error::NoElements)); assert_eq!(iter::once(0).single(), Ok(0)); assert_eq!(iter::repeat(0).single(), Err(single::Error::MultipleElements));
rust
impl<I> Single for I
where I: Iterator