E0312 - 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 E0312

#### Note: this error code is no longer emitted by the compiler.

Reference’s lifetime of borrowed content doesn’t match the expected lifetime.

Erroneous code example:

    #![allow(unused)]
    fn main() {
    pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'static str {
        if maybestr.is_none() {
            "(none)"
        } else {
            let s: &'a str = maybestr.as_ref().unwrap();
            s  // Invalid lifetime!
        }
    }
    }

To fix this error, either lessen the expected lifetime or find a way to not have to use this reference outside of its current scope (by running the code directly in the same block for example?):

    #![allow(unused)]
    fn main() {
    // In this case, we can fix the issue by switching from "static" lifetime to 'a
    pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'a str {
        if maybestr.is_none() {
            "(none)"
        } else {
            let s: &'a str = maybestr.as_ref().unwrap();
            s  // Ok!
        }
    }
    }