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