Skip to content

Borrowing issue with impl Trait leads to unergonomic verbosity #37790

Closed
@Boscop

Description

@Boscop
#![feature(conservative_impl_trait)]

struct A {
    v: Vec<usize>
}
impl A {
	fn foo<'a>(&'a self) -> impl Iterator<Item=usize> + 'a {
		(0..self.v.len()).map(move |i| self.v[i])
	}
}

fn bar() -> A {
    A { v: vec![] }
}

fn baz() -> Vec<usize> {
    /* doesn't work:
    bar().foo().collect::<Vec<_>>()
    */
    
    /* also doesn't work:
    let x = bar();
    x.foo().collect::<Vec<_>>()
    */
    
    // you have to do this:
    let x = bar();
    let x = x.foo();
    x.collect::<Vec<_>>()
}

fn main() {}
error: borrowed value does not live long enough
  --> <anon>:18:5
   |
18 |     bar().foo().collect::<Vec<_>>()
   |     ^^^^^ temporary value created here
...
30 | }
   | - temporary value dropped before borrower
   |
   = note: values in a scope are dropped in the opposite order they are created

error: aborting due to previous error

Here on rust playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions