Efficiently chunk large vector into a vector of vectors
问题 I want to chunk a large vector into a vector of vectors. I know about chunks() , but am not sure of the best way to go from the iterator to a 2D Vec . I have found the following to work, but is there a better way to write this? let v: Vec<i32> = vec![1, 1, 1, 2, 2, 2, 3, 3, 3]; let v_chunked: Vec<Vec<i32>> = v.chunks(3).map(|x| x.to_vec()).collect(); println!("{:?}", v_chunked); // [[1, 1, 1], [2, 2, 2], [3, 3, 3]] https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist