rust - Return a moving window of elements resulting from an iterator of Vec<u8> -


i'm trying figure out how return window of elements vector i've first filtered without copying new vector.

so naive approach works fine think end allocating new vector line 5 don't want do.

let mut buf  = vec::new(); file.read_to_end(&mut buf); // filtering of read file , create new vector subsequent processing let iter = buf.iter().filter(|&x| *x != 10 && *x != 13); let clean_buf = vec::from_iter(iter); iter in clean_buf.windows(13) {     print!("{}",iter.len()); } 

alternative approach use chain()? achieve same thing without copying new vec

for iter in buf.iter().filter(|&x| *x != 10 && *x != 13) {     let window =  ???        } 

you can use vec::retain instead of filter this, allows keep vec:

fn main() {     let mut buf = vec![         8, 9, 10, 11, 12, 13, 14,         8, 9, 10, 11, 12, 13, 14,         8, 9, 10, 11, 12, 13, 14,     ];     println!("{:?}", buf);     buf.retain(|&x| x != 10 && x != 13);     println!("{:?}", buf);     iter in buf.windows(13) {         print!("{}, ", iter.len());     }     println!(""); } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -