Macro ne_vec

Source
macro_rules! ne_vec {
    ($head:expr) => { ... };
    ($head:expr, $($tail:expr),* $(,)?) => { ... };
    ($elem:expr; $n:expr) => { ... };
}
Expand description

Creates a NEVec containing the arguments.

ne_vec! allows NEVecs to be defined with the same syntax as array expressions. There are two forms of this macro:

  • Create a NEVec containing a given list of elements:
use rust2fun::prelude::*;

let nevec = ne_vec![1, 2, 3];
assert_eq!(nevec.head, 1);
assert_eq!(nevec.tail, [2, 3]);
  • Create a NEVec from a given element and size:
use rust2fun::prelude::*;

let nevec = ne_vec![1; 3];
assert_eq!(nevec, [1, 1, 1]);

Note that unlike array expressions this syntax supports all elements which implement Clone and the number of elements doesn’t have to be a constant, but it has to be nonzero.

This will use clone to duplicate an expression, so one should be careful using this with types having a nonstandard Clone implementation. For example, ne_vec![Rc::new(1); 5] will create a vector of five references to the same boxed integer value, not five references pointing to independently boxed integers.

Also, note that ne_vec![expr; 0] is not allowed, and will panic, because it violates the invariant that a NEVec cannot be empty.