I hope that when building Vec through vec! macros, I can expand other arrays and make their elements part of the array, simplifying code writing. The example code is as follows:
let a = vec![3, 4];
let b = vec![5, 6];
let c = vec![1, 2, ...a, ...b]; // then c = [1, 2, 3, 4, 5, 6]
let a = vec![3, 4];
let b = vec![5, 6];
let c = [vec![1, 2], a, b].concat(); // c = [1, 2, 3, 4, 5, 6]
This is what I do today. Might have a couple extra allocations, but most of time it's good enough for me.
I'm not opposing add new vec! syntax though. Do note ..a means RangeTo<Vec<{interger}>> in current Rust, so you can't use this exact syntax, but I guess we can do bikeshedding later.
You can always create your own macro. I used pub since its not valid in expressions, but if you used a proc_macro you could use whatever you want. This should also be more performant than [vec![1, 2], a, b].concat(). (Downside is that this could play poorly with type inference.)
macro_rules! collection {
( $e:expr $(,)? ) => {{
let mut this = Default::default();
// using array, but iter::once could be faster?
Extend::extend(&mut this, [$e]);
this
}};
( pub $e:expr $(,)? ) => {{
let mut this = Default::default();
Extend::extend(&mut this, $e);
this
}};
( $e:expr, $( $t:tt )* ) => {{
let mut this = collection![$e];
collection![this; $( $t )*];
this
}};
( pub $e:expr, $( $t:tt )* ) => {{
let mut this = collection![pub $e];
collection![this; $( $t )*];
this
}};
( $this:ident; $e:expr, $( $t:tt )* ) => {{
Extend::extend(&mut $this, [$e]);
collection![$this; $( $t )*];
}};
( $this:ident; pub $e:expr, $( $t:tt )* ) => {{
Extend::extend(&mut $this, $e);
collection![$this; $( $t )*];
}};
( $this:ident; $( $e:expr)? $(,)? ) => {$({
Extend::extend(&mut $this, [$e]);
})*};
( $this:ident; $( pub $e:expr )? $(,)? ) => {$({
Extend::extend(&mut $this, $e);
})*};
() => { vec![] };
}
// hint to tell the compiler that the output shoule be `Vec<_>`
macro_rules! vec {
( $( $t:tt )* ) => {{
let this: Vec<_> = collection![$( $t )*];
this
}};
}
fn main() {
// works nicely with iterators
let a = b"Hello world".iter().copied();
let b = [4, 5, 6,];
let c = vec![pub a, 3, pub b,];
println!("{:?}", c);
}
You may be interested in Pre-RFC: Unpacking to Array and Tuple Literals I've been working on (though somewhat slowly as of late, due to being busy with dayjob stuff). Although that pre-RFC is principally about unpacking into array and tuple literals, vectors would be a natural follow-up (and are actually mentioned as such at the end).