How to define nested types?

See playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=67e80f454569d61029a849e6b6dc19b6

I have a function similar to this:

fn with_vec_function <I> (_iterable: Vec<(I, usize)>) -> ()
 where I: Clone + Eq + Hash,
 {
 	// ...
}

I would like to generalize this function to work with any iterable, e.g. sets, maps, iterators..

 fn example_function <Sequence, I> (_iterable: Sequence<(I, usize)>) -> ()
 where Sequence: IntoIterator + Clone,
     I: Clone + Eq + Hash,
 {
 	// ...
 }

This won't compile:

error[E0109]: type arguments are not allowed for this type

Is there some trait bound that I am missing?

The issue is that Sequence isn't defined to take any type parameters; you need to move them from the argument list to the where clause:

 fn example_function <Sequence, I> (_iterable: Sequence) -> ()
 where Sequence: IntoIterator<Item=(I,usize)> + Clone,
     I: Clone + Eq + Hash,
 {
 	// ...
 }
1 Like

Thanks :slight_smile:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.