How to bound the type of Iterator::Item?

后端 未结 1 597
[愿得一人]
[愿得一人] 2021-01-11 14:58

I\'m not sure how to specify bounds on the type of the output of the iterator for generic iterators. Before Rust 1.0, I used to be able to do this:

fn somefu         


        
相关标签:
1条回答
  • 2021-01-11 15:15

    You can introduce a second generic type parameter and place the bound on that:

    fn somefunc<A: Int, I: Iterator<Item = A>>(mut xs: I) {
        xs.next().unwrap().pow(2);
    }
    

    You can also place trait bounds on the associated type itself

    fn somefunc<I: Iterator>(mut xs: I)
    where
        I::Item: Int,
    {
        xs.next().unwrap().pow(2);
    }
    
    0 讨论(0)
提交回复
热议问题