Rust lifetime error "implicit elided lifetime not allowed here"

Hey, i am learning Rust so i write some code for practice, i stepped on this problem:
"implicit elided lifetime not allowed here
help: indicate the anonymous lifetime: <'_>rustc(E0726)"

Here is the code: https://repl.it/@josiphaboic/tablegateway
Error is on line 37

Just one small change:

    fn new(connection: &'a Connection) -> UserGateway<'a> {
        UserGateway {
            connection: connection,
        }
    }

You need to indicate that the return type has a field with lifetime 'a. This is what the compiler is telling you when you need to indicate the lifetime.

Even better... just use Self, because you already have the lifetime defined on your impl declaration:

    fn new(connection: &'a Connection) -> Self {
        Self {
            connection
        }
    }

Also, when you have a field name that is equal to the parameter name, you dont have to do: connection: connection. Instead, you can just use connection

1 Like

Thanks, but that did not help, i still get the same error warning in the VS Code.

You forgot lifetime type parameters

//                    vv -- here
impl<'a> TableGateway<'a> for UserGateway<'a> {
    fn new(connection: &'a Connection) -> UserGateway<'a> {
//                                                    ^^ -- and here
2 Likes
impl TableGateway<'_> for UserGateway<'_> {
    // elide the lifetime, since the calling closure of new is implied to last at least as long as Self
    fn new(connection: &Connection) -> Self {
        Self {
            connection,
        }
    }
}
1 Like

Thanks! This worked :slight_smile:

1 Like