Content-Length: 24576 | pFad | https://doc.rust-lang.org/std/iter/../clone/../../../std/../error_codes/./E0491.html

E0491 - Error codes index

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 E0491

A reference has a longer lifetime than the data it references.

Erroneous code example:

#![allow(unused)]
fn main() {
struct Foo<'a> {
    x: fn(&'a i32),
}

trait Trait<'a, 'b> {
    type Out;
}

impl<'a, 'b> Trait<'a, 'b> for usize {
    type Out = &'a Foo<'b>; // error!
}
}

Here, the problem is that the compiler cannot be sure that the 'b lifetime will live longer than 'a, which should be mandatory in order to be sure that Trait::Out will always have a reference pointing to an existing type. So in this case, we just need to tell the compiler than 'b must outlive 'a:

#![allow(unused)]
fn main() {
struct Foo<'a> {
    x: fn(&'a i32),
}

trait Trait<'a, 'b> {
    type Out;
}

impl<'a, 'b: 'a> Trait<'a, 'b> for usize { // we added the lifetime enforcement
    type Out = &'a Foo<'b>; // it now works!
}
}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://doc.rust-lang.org/std/iter/../clone/../../../std/../error_codes/./E0491.html

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy