Frame variable lifetime issue


pub struct Language {
    _abbrev: String,
    _data: Arc<LanguageData>,
    _region: Option<Region>,

impl Language {
    fn parse_tag<T: ToString>(tag: T) -> Option<(String, String)> {
        if region_abbrev.is_none() {
            let r = Region::parse(language_abbrev);
            if let Some(a) = r {
                region_abbrev = Some(a.to_string().to_lowercase().as_str());

In the range of code a.to_string().to_lowercase() I'm getting:

temporary value dropped while borrowed
consider using a `let` binding to create a longer lived valuerustcE0716, 76): temporary value is freed at the end of this statement, 12): borrow later used here

I did try binding this range to a variable b, but the issue persists:

let b = a.to_string().to_lowercase();
region_abbrev = Some(b.as_str());

With this the error goes on the entire b.as_str() range.

Next time please:

  • paste the entire error message. It's hard to figure out what the error points to if you only provide the message, not the line numbers or any other context.
  • post a complete, minimal example on the Playground instead of cutting off arbitrary, valuable pieces of the code.
  • don't start field names with an underscore. Leading underscore means "this variable is unused".

Accordingly, I don't see what the type of region_abbrev is or where it is declared/supposed to be used. Anyway, the string returned from to_lowercase() is a temporary, so you can't store a reference to it (i.e., you can't store the result of .as_str()), because it would be invalidated when the temporary is dropped.

1 Like

Sorry for lack of information. Then, since .as_str() returns a temporary value, I need to change region_abbrev's type. region_abbrev is a Option<&str> value, so I think I need to change it to Option<String> in my case.

Yes, that's probably right.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.