How to take vector inside vector out?

This holds the vector
let mut ks: Vec<Vec<usize>> = Vec::new();
and when I print out ks I get [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]. How do I transform the vector to get [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Try flatten

fn main() {
    let myvec: Vec<Vec<usize>> = vec![vec![0usize,1,2,3,4], vec![10usize,11,12,13,14]];
    println!("Before: {:?}", &myvec);
    let flattened: Vec<usize> = myvec.into_iter().flatten().collect();
    println!("After: {:?}", &flattened);
}

playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=14a93db1fbfbe78ee29ffb101a645e5a

Result:

Before: [[0, 1, 2, 3, 4], [10, 11, 12, 13, 14]]
After: [0, 1, 2, 3, 4, 10, 11, 12, 13, 14]
1 Like