Ergonomics initiative discussion: Allowing owned values where references are expected

I’d mostly like to add a usecase — allowing a function that accepts a reference to be implicitly converted when used as a Fn* trait:

struct Foo;

impl Foo {
    fn with_ref(&self) -> u8 { 42 }
    fn with_val(self) -> u8 { 42 }
}

fn main() {
    let a = Some(Foo);

    a.map(|f| f.with_val());
    a.map(Foo::with_val);

    a.map(|f| f.with_ref());
    a.map(Foo::with_ref); // Does not compile
}

I feel that this code appears inconsistent. I can’t just automatically see (|x| x.foo()) and replace it with (X::foo).

2 Likes