I'm having trouble unwrapping Options and converting the values to string slices.
Some value, I want to convert it to a &str.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
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(),
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With