`impl Trait` for local variables

Annotating variables with "impl Trait" could be helpful to annotate futures or streams that you don't know the exact type of:

fn process_stream(stream: impl Stream<Item = u64>) -> impl Stream<Item = String> {
  let intermediate_result: impl Stream<Item = String> =
      stream.map(|item| item.to_string());
  intermediate_result.map(|item| format!("Prefix {} Suffix", item))
}

This example is quite simple and here we wouldn't get much benefit, but for longer filter/map chains, annotating intermediate result types can be quite helpful.

Are there plans to allow this?

2 Likes

Yes, the full existential type RFC has not been implemented yet. The tracking issue for it in bindings is here, and the general meta tracking issue is here. impl_trait_in_bindings is actually implemented and available on nightly:

#![feature(impl_trait_in_bindings)]

fn main() {
    let x: impl Trait = foo;
}
13 Likes