3 ms·
> vec![] => exclamation mark squeezed between what appears to be a keyword "vec" and an array syntax vec![1,2,3] declares a vector of [1,2,3]. But it isn't `ke
by eximius 7y ago
> vec![] => exclamation mark squeezed between what appears to be a keyword "vec" and an array syntax
vec![1,2,3] declares a vector of [1,2,3]. But it isn't `keyword` ! `array`, it's more like vec(1,2,3) where vec is a variadic function. But instead of being a function, its a macro so its done at compile time using AST transformations.
Basically, `vec` isn't special, `!` is. `anything!()` or `anything![]` are ways to call macros.
I don't recall the exact transformation, but vec![1,2,3] is basically equal to
let v = Vec::new();
v.push(1);
v.push(2);
v.push(3);
It's not quite equal because it might initialize with Vec::with_capacity(N) where N is the number of args, but you get the point.