Christopher Plewright
Christopher Plewright

Reputation: 471

How to return a &str, avoiding "temporary value dropped while borrowed"

I'm having trouble unwrapping Options and converting the values to string slices.

For example:

let val: Option<u8> = row.get(k);
match val {
    None => "",
    Some(v) => v.to_string().as_str()
}

But this fails with errors like this.

temporary value dropped while borrowed
consider using a `let` binding to create a longer lived value rustc E0716
main.rs(.....): temporary value is freed at the end of this statement

I have tried breaking it down in various different ways, for example like this

let val: Option<u8> = row.get(k);
match val {
    None => "",
    Some(v) => {
        let vstr = v.to_string();
        vstr.as_str()
    }
}

But I'm still really stuck. I've looked through a many similar problems but none are quite the same, and I'm still stuck. How can I do this?

Clarification, I want the decimal representation of the tinyint, not the character point. This is not utf8 string, just a number.

let x: u8 = 10;
let s: String = x.to_string();
println!("10 = {}", s);

This should print 10 = 10

Upvotes: 3

Views: 2019

Answers (2)

user4815162342
user4815162342

Reputation: 154996

In the more general case, when you cannot keep the string around, you could use Cow:

let val: Option<u8> = row.get(k);
let msg = match val {
    None => Cow::Borrowed(""),
    Some(v) => Cow::Owned(v.to_string()),
};

Now msg holds either a borrowed or an owned string, depending on the situation.

Upvotes: 0

bk2204
bk2204

Reputation: 76509

If you're trying to use the &str in the same function, you can hoist the declaration so that the string lives long enough:

let val: Option<u8> = row.get();
let vstr;
let msg = match val {
    None => "",
    Some(v) => {
        vstr = v.to_string();
        vstr.as_str()
    }
};
println!("value is {}", msg);

If your goal is to return the &str to a caller, then that's not possible. That's a borrowed value, and the string that owns the memory won't live past the end of the function at most. You can either return String or Cow<'static, str>.

For example, you could do this:

let val: Option<u8> = row.get();
match val {
    None => "".to_owned(),
    Some(v) => v.to_string(),
}

or this:

let val: Option<u8> = row.get();
match val {
    None => "".into(),
    Some(v) => v.to_string().into(),
}

Upvotes: 4

Related Questions