I want to write a function that receives a &str
argument and returns Option<&str>
. I wrote this:
fn f(text: &str) -> Option<&str> {
if // some condition {
return None;
}
let mut res = String::new();
// write something into res.
// return res
Some(&res[..])
}
but I get an error:
res
does not live long enough.
What is the best solution to fix this?