/// Name extracted from the subject.
/// May return an empty string if there's no subject.
pub fn name(&self) -> &str {
let re1 = regex!(r#"..."#);
let re2 = regex!(r"...");
if let Some(captured) = re1.captures(&self.subject) {
return captured[1].trim();
}
if let Some(captured) = re2.captures(&self.subject) {
return captured[1].trim();
}
""
}
The struct looks like this:
pub struct File {
pub subject: String,
/// more fields but not relevant here
}
Errors:
error[E0515]: cannot return value referencing local variable `captured`
--> src\lib.rs:122:20
|
122 | return captured[1].trim();
| --------^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `captured` is borrowed here
Aside from the error, I also have a question on whether I should return an empty string or say an Option? In my original python code this was:
@cached_property
def name(self) -> str:
if parsed := re.search(r'"..."', self.subject):
return parsed.group(1).strip()
elif parsed := re.search(r"...", self.subject):
return parsed.group(1).strip()
else:
return ""