How can I zip more than two iterators?

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

提交回复
热议问题