Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust: Vec<Vec<T>> into Vec<T>

Tags:

rust

let a = vec![ vec![1, 2], vec![3, 4], vec![5, 6] ];

How can I gather into a single Vec all the values contained in all the Vecs in a ?

like image 602
op325 Avatar asked Apr 18 '20 10:04

op325


1 Answers

You can use the flatten operator to remove the nesting of the vectors.

The following example is taken from the link.

let data = vec![vec![1, 2, 3, 4], vec![5, 6]];
let flattened = data.into_iter().flatten().collect::<Vec<u8>>();
assert_eq!(flattened, &[1, 2, 3, 4, 5, 6]);
like image 71
Steve Avatar answered Oct 12 '22 23:10

Steve