Crate vec1[−][src]
This crate provides a Vec
wrapper (Vec1
) which guarantees to have at last 1 element.
This can be useful if you have a API which accepts one ore more ofe a kind.
Instead of accepting a Vec
and returning an error if it's empty a Vec1
can be used assuring there is at last 1 element and through this reducing
the number of possible error causes.
Example
#[macro_use] extern crate vec1; use vec1::Vec1; fn main() { // vec1![] makes sure at compiler time // there is at last one element //let names = vec1! [ ]; let names = vec1! [ "Liz" ]; greet(names); } fn greet(names: Vec1<&str>) { // methods like first/last which return a Option on Vec do // directly return the value, we know it's possible let first = names.first(); println!("hallo {}", first); for name in names.iter().skip(1) { println!(" who is also know as {}", name) } }
Macros
vec1 |
a macro similar to |
Structs
Size0Error |
Error returned by operations which would cause Vec1 to have a len of 0 |
Vec1 |
|