macro_rules! try_vec_in {
($allocator:expr) => { ... };
($elem:expr; $n:expr => $allocator:expr) => { ... };
($($x:expr),+ $(,)? => $allocator:expr) => { ... };
}Expand description
Creates a Vec containing the arguments with the provided allocator.
try_vec_in! allows Vecs to be defined with the same syntax as array expressions.
There are two forms of this macro:
- Create a
Veccontaining a given list of elements:
#![feature(allocator_api)]
use std::alloc::System;
let v = try_vec_in![1, 2, 3 => System]?;
assert_eq!(v[0], 1);
assert_eq!(v[1], 2);
assert_eq!(v[2], 3);- Create a
Vecfrom a given element and size:
#![feature(allocator_api)]
use std::alloc::System;
let v = try_vec_in![1; 3 => System]?;
assert_eq!(v, [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.
This will use clone to duplicate an expression, so one should be careful
using this with types having a nonstandard Clone implementation. For
example, try_ve_in![Rc::new(1); 5 => allocator] will create a vector of five references
to the same boxed integer value, not five references pointing to independently
boxed integers.
Also, note that try_vec_in![expr; 0 => allocator] is allowed, and produces an empty vector.
This will still evaluate expr, however, and immediately drop the resulting value, so
be mindful of side effects.