New lint: slow or unsafe vector initialization · Issue #3237 · rust-lang/rust-clippy (original) (raw)
Cross post of rust-lang/rust#54628. See the thread for more details. The content below is a dumb summary.
Bad example
// resize
let mut vec1 = Vec::with_capacity(len);
vec1.resize(len, 0);
// extend
let mut vec2 = Vec::with_capacity(len);
vec2.extend(repeat(0).take(len))
Also some unsafe approaches exist, but it's TBD.
Good example
// vec! macro
let mut vec3 = vec![0; len];