You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
instead of calling vec![] to create an empty vec, use Vec::new(). the vec! macro's purpose is for adding any number of items to a vec in an ergonomic and optimized way, so there is no advantage to creating an empty vec with a macro.
vec![] will expand to Vec::new() anyway, so just write Vec::new().
Advantage
remove unnecessary macro call for times you just need an empty vec
-- not sure if this counts as a micro/nano-optimization since there no more macro expansion
-- at the very least, i think it's good to reserve macros for only when they provide some value
Drawbacks
No response
Example
let nums:Vec<usize> = vec![];
Could be written as:
let nums:Vec<usize> = Vec::new();
The text was updated successfully, but these errors were encountered:
I don't think this should be more than a restriction lint. There is nothing clearer and more readable about new imo, especially when there are several other uses of the vec macro with values in the same function, it's more consistent.
What it does
instead of calling
vec![]
to create an empty vec, useVec::new()
. thevec!
macro's purpose is for adding any number of items to a vec in an ergonomic and optimized way, so there is no advantage to creating an empty vec with a macro.vec![]
will expand toVec::new()
anyway, so just writeVec::new()
.Advantage
-- not sure if this counts as a micro/nano-optimization since there no more macro expansion
-- at the very least, i think it's good to reserve macros for only when they provide some value
Drawbacks
No response
Example
Could be written as:
The text was updated successfully, but these errors were encountered: