How to constrain the element type of an iterator?

前端 未结 3 1351
离开以前
离开以前 2021-01-19 04:15

I’m converting some older Rust code to work on 1.0.0. I need to convert a function that takes an iterator over characters, which used to be written like this:



        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 04:42

    fn f>(char_iter: I)
    

    Associated types were recently added to the language, and many library types were updated to take advantage of them. For example, Iterator defines one associated type, named Item. You can add a constraint on the associated type by writing the name of the associated type, an equals sign, and the type you need.

提交回复
热议问题