Since you are changing the size of the Vec
, it's likely that it will need reallocation anyway. Additionally, you cannot mutate a Vec
while you are iterating it (precisely because mutating it could cause it to reallocate). It won't be much different to collect into a new Vec
:
myvec = myvec
.drain(..)
.flat_map(|val| iter::once(val + 2).chain(iter::once(val + 4)))
.collect();
The chained iterator might not be as optimal as it could be. In nightly Rust, you could do:
#![feature(array_value_iter)]
use std::array;
myvec = myvec
.drain(..)
.flat_map(|val| array::IntoIter::new([val + 2, val + 4]))
.collect();
Which should be more efficient.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…