non_empty_vec

Macro ne_vec

Source
macro_rules! ne_vec {
    () => { ... };
    ($($x:expr),+ $(,)?) => { ... };
    ($elem:expr; 0) => { ... };
    ($elem:expr; $n:literal) => { ... };
    ($elem:expr; $n:expr) => { ... };
}
Expand description

Constructs a NonEmpty vector, similar to std’s vec macro.

This macro will generally try to check the validity of the length at compile time if it can.

If the length is an expression (e.g. ne_vec![(); { 0 }]), the check is performed at runtime to allow the length to be dynamic.

§Examples

Proper use.

assert_eq!(
    ne_vec![1, 2, 3],
    NonEmpty::try_from(vec![1, 2, 3_i32]).unwrap(),
);

assert_eq!(
    ne_vec![1; 3],
    NonEmpty::try_from(vec![1, 1, 1]).unwrap(),
);

Improper use.

let _ = ne_vec![];
let _ = ne_vec![1; 0];
let _ = ne_vec![1; 0usize];
let n = 0;
let _ = ne_vec![1; n];