16

I want to write a function that accepts Iterator of type that has ToString trait.

What I have in mind:

fn parse<T: Iterator /* ?T::Item : ToString? */>(mut args: T) -> Result<String, String> {
    match args.next() {
        Some(x) => x.to_string(),
        None => String::from("Missing parameter"),
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
nothrow
  • 15,882
  • 9
  • 57
  • 104

2 Answers2

22

Yes, you can do that with a where clause:

fn parse<T: Iterator>(mut args: T) -> Result<String, String>
where 
    <T as Iterator>::Item: ToString,
{
   // ....
}

Or, since it's unambiguous which Item is meant here, the bound can just be:

where T::Item: ToString
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
  • 1
    Thanks! How is the `::Item` part called? I was not able to google it, since I was probably missing the correct terminology. – nothrow Jan 18 '19 at 17:00
  • 1
    That part doesn't have a name by itself (I don't think), but `T::Item: ToString` is called a _bound_, or a _trait bound_. You might also see it called a _constraint_. – Peter Hall Jan 18 '19 at 17:03
13

You can use the Item = syntax:

fn parse<I: ToString, T: Iterator<Item = I>>(mut args: T) -> Result<String, String>

That allows you to simplify this further with the impl syntax:

fn parse<T: Iterator<Item = impl ToString>>(mut args: T) -> Result<String, String>

and finally:

fn parse(mut args: impl Iterator<Item = impl ToString>) -> Result<String, String>

I would consider this a more readable alternative.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Chronial
  • 66,706
  • 14
  • 93
  • 99