Jen
Jen

Reputation: 1338

What is the difference between vec![] and vec!()

I found out that I can use both vec![] and vec!() in my code and they both do the same -- initialize a Vec. I found no documentation on the later form though. Is it indeed the same thing? Which one should I use? Why?

Upvotes: 1

Views: 966

Answers (2)

Jen
Jen

Reputation: 1338

In fact there are 3 forms (), [] and {} and they're all identical.

https://users.rust-lang.org/t/braces-square-brackets-and-parentheses-in-macro-call/9984/2

Yes, they are identical. Use whichever you like best. Usually () is for function-like macros, [] is used for vec![…], and {} is for other cases.

Upvotes: 0

Masklinn
Masklinn

Reputation: 42332

Macros can be invoked using [], (), or {}.

Which delimiter is used makes no actual difference to the way it executes, though usually people will use braces for “block-like” or “definition” macros (e.g. tokio’s select), brackets for literal-like (vec) and parens for function-like expressions (e.g. println or matches).

Upvotes: 7

Related Questions