How can I zip more than two iterators?

前端 未结 3 937
南笙
南笙 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:23

    You could also create a macro using the .zip provided like,

    $ cat z.rs
    macro_rules! zip {
        ($x: expr) => ($x);
        ($x: expr, $($y: expr), +) => (
            $x.iter().zip(
                zip!($($y), +))
        )
    }
    
    
    fn main() {
        let x = vec![1,2,3];
        let y = vec![4,5,6];
        let z = vec![7,8,9];
    
        let zipped = zip!(x, y, z);
        println!("{:?}", zipped);
        for (a, (b, c)) in zipped {
            println!("{} {} {}", a, b, c);
        }
    }
    

    Output:

    $ rustc z.rs && ./z
    Zip { a: Iter([1, 2, 3]), b: Zip { a: Iter([4, 5, 6, 67]), b: IntoIter([7, 8, 9]), index: 0, len: 0 }, index: 0, len: 0 }
    1 4 7
    2 5 8
    3 6 9
    
    0 讨论(0)
  • 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<T: Clone>(records: &Vec<Vec<T>>) -> Vec<Vec<T>> {
        let mut transposed: Vec<Vec<T>> = vec![Vec::new(); records[0].len()];
    
        for record in records {
            for (index, element) in record.iter().enumerate() {
                transposed[index].push(element.clone());
            }
        }
    
        transposed
    }
    
    0 讨论(0)
  • 2020-12-29 21:37

    You can use the izip!() macro from the crate itertools, which implements this for arbitrary many iterators:

    use itertools::izip;
    
    fn main() {
    
        let a = [1, 2, 3];
        let b = [4, 5, 6];
        let c = [7, 8, 9];
    
        // izip!() accepts iterators and/or values with IntoIterator.
        for (x, y, z) in izip!(&a, &b, &c) {
    
        }
    }
    

    You would have to add a dependency on itertools in Cargo.toml, use whatever version is the latest. Example:

    [dependencies]
    itertools = "0.8"
    
    0 讨论(0)
提交回复
热议问题