Non-empty variants of the standard collections.
Non-emptiness can be a powerful guarantee. If your main use of Vec
is as
an Iterator
, then you may not need to distinguish on emptiness. But there
are indeed times when the Vec
you receive as a function argument needs to
be non-empty or your function can't proceed. Similarly, there are times when
the Vec
you return to a calling user needs to promise it actually contains
something.
With NEVec
, you're freed from the boilerplate of constantly needing to
check is_empty()
or pattern matching before proceeding, or erroring if you
can't. So overall, code, type signatures, and logic become cleaner.
Consider that unlike Vec
, [NEVec::first
] and [NEVec::last
] don't
return in Option
; they always succeed.
Alongside [NEVec
] are its cousins [NEMap
] and [NESet
]; Hash Maps and
Hash Sets which are guaranteed to contain at least one item.
The simplest way to construct these non-empty collections is via their
macros: [nev
], [nes
], and [nem
]:
```rust use nonempty_collections::*;
let v: NEVec
Unlike the familiar vec!
macro, nev!
and friends require at least one
element:
```rust use nonempty_collections::nev;
let v = nev![1];
// Doesn't compile! // let v = nev![]; ```
Like Vec
, you can also construct a [NEVec
] the old fashioned way with
[NEVec::new
] or its constructor:
```rust use nonempty_collections::NEVec;
let mut l = NEVec { head: 42, tail: vec![36, 58] }; assert_eq!(l.head, 42);
l.push(9001); assert_eq!(l.last(), &9001); ```
And if necessary, you're free to convert to and from Vec
:
```rust use nonempty_collections::{NEVec, nev};
let l: NEVec
let u: Option
This library extends the notion of non-emptiness to Iterators, and provides
the [NonEmptyIterator
] trait. This has some interesting consequences:
map
preserve non-emptiness.max
always have a result.collect
ed back into a non-empty structure.```rust use nonempty_collections::*;
let v: NEVec<_> = nev![1, 2, 3].intononemptyiter().map(|n| n + 1).collect(); assert_eq!(2, v.head); ```
Since NEVec
, NEMap
, and NESet
must have a least one element, it is not
possible to implement the [FromIterator
] trait for them. We can't know, in
general, if any given standard-library [Iterator
] actually contains
something.
serde
: serde
support.