Hello,
I'm rather new to Rust and I'm stumbling on what I think is a lifetime issue.
In playing around with the Regex crate, I tried to implement the following:
trait QuickCapture {
fn cap<'a>(&self, i: usize) -> Option<&'a str>;
}
impl<'t> QuickCapture for regex::Captures<'t> {
fn cap<'a>(&self, i: usize) -> Option<&'a str> {
let got = self.get(i)?;
Some(got.as_str())
}
}
This gives me a rather long error saying "...the lifetime cannot outlive the lifetime 't
...".
If I remove the 'a and 't lifetimes and instead use an anonymous lifetime for Captures, the code does compile, but I get an error when I go to use it.
fn parse_thing(thing_string: &str) -> Option<&str> {
let re = Regex::new(r"thing: (.*)").unwrap();
let caps = re.captures(thing_string)?;
caps.cap(1)
}
results in the error
error[E0515]: cannot return value referencing local variable `caps`
--> src/main.rs:18:5
|
18 | caps.cap(1)
| ----^^^^^^^
| |
| returns a value referencing data owned by the current function
| `caps` is borrowed here
From my understanding, re.captures takes in a &'t str and returns a Captures<'t>, and all the calls through to the as_str() in the cap() method should have the same lifetime. My assumption was that the &str returned in the Option from the cap() method should live at least as long as the &str passed into parse_thing().
Am I totally wrong here? How would I express something like this?
I've put together a little sample in the Rust Playground:
Thanks!