E0623 - Error codes index
Description: Book listing all Rust error codes

## Keyboard shortcuts

Press `←` or `→` to navigate between chapters

Press `S` or `/` to search in the book

Press `?` to show this help

Press `Esc` to hide this help

# Error code E0623

A lifetime didn’t match what was expected.

Erroneous code example:

    #![allow(unused)]
    fn main() {
    struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
    where
        T: Convert<'a, 'b>;

    trait Convert<'a, 'b>: Sized {
        fn cast(&'a self) -> &'b Self;
    }
    impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
        fn cast(&'long self) -> &'short T {
            self
        }
    }
    // error
    fn badboi<'in_, 'out, T>(
        x: Foo<'in_, 'out, T>,
        sadness: &'in_ T
    ) -> &'out T {
        sadness.cast()
    }
    }

In this example, we tried to set a value with an incompatible lifetime to another one (`'in_` is unrelated to `'out`). We can solve this issue in two different ways:

Either we make `'in_` live at least as long as `'out`:

    #![allow(unused)]
    fn main() {
    struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
    where
        T: Convert<'a, 'b>;

    trait Convert<'a, 'b>: Sized {
        fn cast(&'a self) -> &'b Self;
    }
    impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
        fn cast(&'long self) -> &'short T {
            self
        }
    }
    fn badboi<'in_: 'out, 'out, T>(
        x: Foo<'in_, 'out, T>,
        sadness: &'in_ T
    ) -> &'out T {
        sadness.cast()
    }
    }

Or we use only one lifetime:

    #![allow(unused)]
    fn main() {
    struct Foo<'a, 'b, T>(std::marker::PhantomData<(&'a (), &'b (), T)>)
    where
        T: Convert<'a, 'b>;

    trait Convert<'a, 'b>: Sized {
        fn cast(&'a self) -> &'b Self;
    }
    impl<'long: 'short, 'short, T> Convert<'long, 'short> for T {
        fn cast(&'long self) -> &'short T {
            self
        }
    }
    fn badboi<'out, T>(x: Foo<'out, 'out, T>, sadness: &'out T) -> &'out T {
        sadness.cast()
    }
    }