MB
for _ in 0..2 {} будет чище
Size: a a a
MB
Э
let mut i = 0;Возможно ли сократить такой код, без использования retain?
while i < 2 {
if let Some(index) = vec.iter().position(|&x| x == 0 || x == 4) {
vec.remove(index);
}
i += 1;
}
(0..2).for_each(|_| if let Some(index) = vec.iter().position(|&x| x == 0 || x == 4) {
vec.remove(index);
})Z
(0..2).for_each(|_| if let Some(index) = vec.iter().position(|&x| x == 0 || x == 4) {
vec.remove(index);
})Э
vec.iter() вместо vec[last_found_pos..].iter() не нравится и то, что если в первую итерацию ничего не нашлось, вторая всё равно будет.Э
Vec::remove или Vec::swap_remove сойдёт?Z
vec.iter() вместо vec[last_found_pos..].iter() не нравится и то, что если в первую итерацию ничего не нашлось, вторая всё равно будет.Э
MB
Э
if let Some(index) = vec.iter().position(|&x| x == 0 || x == 4) {
vec.remove(index);
if let Some(index) = vec[index..].iter().position(|&x| x == 0 || x == 4) {
vec.remove(index);
}
}MB
MB
if let Some(index) = vec.iter().position(|&x| x == 0 || x == 4) {
vec.remove(index);
if let Some(index) = vec[index..].iter().position(|&x| x == 0 || x == 4) {
vec.remove(index);
}
}Э
Z
MB
Э
Vec::swap_remove, читай описание.MB
Z
Z
Э

MB