Hi
I'm desperately trying to understand why this code is not working:
extern crate regex;
use regex::Regex;
fn kaputt<'wtf>() -> Result<&'wtf str, &'static str> {
let mut data: Vec<u8> = vec![60, 104, 101, 108, 108, 111, 62, 119, 111, 114, 108, 100, 60, 47, 104, 101, 108, 108, 111, 62, ];
let re = Regex::new(r"<hello>(?P<foo>.*)</hello>").unwrap();
let foo = String::from_utf8_lossy(&data);
match re.captures(&foo.to_string()) {
Some(m) => Ok(m.name("foo").map_or("", |m| m.as_str())),
_ => Err("kaputt"),
}
}
fn main() {
let blah = kaputt();
println!("{:?}", blah);
}
error[E0597]: borrowed value does not live long enough
--> src/main.rs:12:24
|
12 | match re.captures(&foo.to_string()) {
| ^^^^^^^^^^^^^^^ does not live long enough
...
16 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'wtf as defined on the function body at 5:1...
--> src/main.rs:5:1
|
5 | / fn kaputt<'wtf>() -> Result<&'wtf str, &'static str> {
6 | | let mut data: Vec<u8> = vec![60, 104, 101, 108, 108, 111, 62, 119, 111, 114, 108, 100, 60, 47, 104, 101, 108, 108, 111, 62, ];
7 | |
8 | | let re = Regex::new(r"<hello>(?P<foo>.*)</hello>").unwrap();
... |
15 | | }
16 | | }
| |_^