[][src]Crate vec1

This crate provides a Vec wrapper (Vec1) which guarantees to have at least 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 least 1 element and through this reducing the number of possible error causes.

The crate provides an optional serde feature, which provides implementations of serde::Serialize/serde::Deserialize.

Example

#[macro_use]
extern crate vec1;

use vec1::Vec1;

fn main() {
    // vec1![] makes sure at compiler time
    // there is at least 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 vec! to create a Vec1.

Structs

Size0Error

Error returned by operations which would cause Vec1 to have a length of 0.

Vec1

std::vec::Vec wrapper which guarantees to have at least 1 element.