Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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

  • When the Option has Some value, I want to convert it to a &str.
  • When the Option is None, I simply return an empty string slice "".

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

like image 786
Christopher Plewright Avatar asked Jan 31 '26 20:01

Christopher Plewright


1 Answers

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(),
}
like image 77
bk2204 Avatar answered Feb 03 '26 10:02

bk2204