Non-empty vectors (vec1
)
See the official docs for more info.
Why do we use Vec1
instead of Vec
? Can't we just assume that all our vectors
are not empty? Well, we could, but then:
-
We'd probably be wrong at some point in the future;
-
We're making the code inject panic code somewhere it probably doesn't need to; and
-
We can do better than this.
This article, although catered for Haskell, presents the case well. Put simply, if we require something to be non-empty, then we can express that with a type, and this means we don't need to re-validate its non-empty-ness after its creation.
We can also avoid using Vec
by using Option<Vec1>
; we still need to check
whether we have Some
or None
, but doing so is better than assuming a Vec
is non-empty.