What are the for in types in Rust?

Currently (1.3.0) rustc aссepts the following:

type T = for <'lt> fn (i32) -> i32;

What is this thing? :slight_smile: I failed to find anyting about it in the reference, so I guess that it is some obsolete syntax, but I'm not sure.


Not at all! It's actually very useful in some cases. It says "for any lifetime 'lt ...", which is especially useful when using closures where you don't want to tie a lifetime to anything else: <F: for<'a> Fn(A<'a>, B<'a>) -> C>, compared to <'a, F: Fn(A<'a>, B<'a>) -> C>. An other case could be a where clause, like this:

impl<T> MyTrait for T where for<'a> &'a T: IntoIterator { ... }

This implements MyTrait for any type T, where a reference to it can be turned into an iterator. We don't care about the lifetime of the reference, so we will just specify it in that single predicate.


This is called a higher ranked trait bound. I discovered them after I asked this question.

1 Like

Good description. I propose to write a separate book (after Rustonomicon) where we collect similar things.

I think Rust Ruses Book is a great name for that )

1 Like