How can I zip more than two iterators?

前端 未结 3 968
南笙
南笙 2020-12-29 20:32

Is there a more direct and readable way to accomplish the following:

fn main() {
    let a = [1, 2, 3];
    let b = [4, 5, 6];
    let c = [7, 8, 9];
    let         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 21:31

    I wanted to be able to do this to an arbitrarily long vector, so I had to implement this by hand:

    fn transpose_records(records: &Vec>) -> Vec> {
        let mut transposed: Vec> = vec![Vec::new(); records[0].len()];
    
        for record in records {
            for (index, element) in record.iter().enumerate() {
                transposed[index].push(element.clone());
            }
        }
    
        transposed
    }
    

提交回复
热议问题