我想将数字转换为字符串。有时,该数字不存在,应该用空字符串表示。
我尝试编写以下代码:
struct Example {
number: Option<usize>,
}
impl Example {
fn example(&self) {
let result = match self.number {
Some(num) => num.to_string().as_str(),
None => "",
};
}
}
fn main() {}
但是,借阅检查员不允许这样做:
error[E0597]: borrowed value does not live long enough
--> src/main.rs:8:49
|
8 | Some(num) => num.to_string().as_str(),
| --------------- ^ temporary value dropped here while still borrowed
| |
| temporary value created here
...
11 | }
| - temporary value needs to live until here
如何正确地/惯用地写这个?