How to write iterator adapter

Any advice where I can learn how to use rust generics? Documentation explain the basics but does not teach how to solve even basic tasks, because it is so radical departure from OOP, I can't rewire my brain using those basic examples in the doc.

Yeah, the docs are quite light on the topic of actually using generics. Have you seen the chapter in the new book http://rust-lang.github.io/book/ch10-00-generics.html? It is not complete yet, but it may contain some useful examples! What works for me to learn more advanced features of Rust is asking questions at users.rust-lang.org :slight_smile:

If you know something about C++ templates or Haskell typeclasses it may be useful to contrast it with Rust traits. There are no close analogs in the OOP world. Also If you happen to know Russian you might find this fragment of my talk helpful: https://www.youtube.com/watch?v=TiCSWNpeR9Q&feature=youtu.be&t=48m25. I don't "explain" generics there, but I try to contrast them with OOP features.

To my eye those 2 constructs declare the same intention.

Haha, this is the exact same problem I once faced myself: Confusion about impls without for :slight_smile: There's a surprising syntactic "overload" for the traits.

If we have a trait Foo, then we can use it as a bound for some type parameter T. That is, you use T as a type name, and write T: Foo in < > or the where clause. This can be read as "for all types T that implement Foo, ...".

However, and here's the catch, you can also use Foo as a type name! And in the type position, the name of the trait means a specific concrete type: trait object: Trait Objects.

So your second example implements TuplesImpl for a potentially unbounded number of types, and your first example implements TuplesImpl only for Iterator trait objects.

1 Like