3 ms·
What would be a good example of a type the follows the rule from the article but where the associated type is not the return type?
by weinzierl 2y ago
What would be a good example of a type the follows the rule from the article but where the associated type is not the return type?
- sciolizer 2y agoGood question. Technically, ToOwned counts: pub trait ToOwned { type Owned: Borrow<Self>; fn clone_into(&self, target: &mut Self::Owned) { ... } } Here the `Owned` is technically an input to `clone_into`, but of course semantically it's still an output. A more subtle one is `StreamExt`: pub trait StreamExt: Stream { type Item; fn map<T, F>(self, f: F) -> Map<Self, F> where F: FnMut(Self::Item) -> T, Self: Sized { ... } } Here, the associated type `Item` is the input to the mapping function, and since the mapping function is an input to the map function, it is an input-to-an-input - which basically makes it an output. i.e. the stream "outputs" the items into the mapping function. (aka two contravariants make a covariant). I couldn't find any more direct examples.