Skip to content

Allow impl Trait in more contexts #1879

@spinda

Description

@spinda

Currently impl Trait is only allowed as the return type of a free-standing functions and inherent methods. I would like to see it opened to a few more positions.

Local variable signatures

let x: impl Foo = bar();

can already be simulated by:

fn wrapper<T: Foo>(x: T) -> impl Foo { x }
let x = wrapper(bar());

Also,

let x: Option<impl Foo> = bar();

If bar returns Option<Baz>, Baz would be checked for an implementation of Foo.

Parameters of free-standing functions and methods

fn bar(x: impl Foo) { }

can be equivalent to:

fn bar<T: Foo>(x: T) { }

Furthermore,

fn bar(x: Option<impl Foo>) { }

can be equivalent to:

fn bar<T: Foo>(x: Option<T>) { }

This cuts down a bit on boilerplate.

Other positions in return types of free-standing functions and methods

fn bar() -> Option<impl Foo> { }

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions