[E0515] I don't really understand lifetimes
Python programmer here. The way I understand lifetimes is that they're very similar to scope, if not the same.
I'm tryna make a function to extract a SSID profile's password, when I was greeted with E0515.
fn get_password(profile: &String) -> Result<Match, String> {
let output = Command::new("netsh")
.args(["wlan", "show", "profile", profile, "key=clear"])
.output();
if output.is_err() {
Err(output.unwrap_err().to_string())
} else {
let res = String::from_utf8(output.unwrap().stdout).unwrap();
let parser = Regex::new(&SSID_KEY_REGEX).unwrap();
Ok(parser.find(&res).unwrap())
}
}
I think it says that res's lifetime is too short, but I don't know how to "lengthen" that lifetime, to put it in words.
error[E0515]: cannot return value referencing local variable `res`
--> src/main.rs:70:9
|
70 | Ok(parser.find(&res).unwrap())
| ^^^^^^^^^^^^^^^----^^^^^^^^^^^
| | |
| | `res` is borrowed here
| returns a value referencing data owned by the current function
How do I make it so that res doesn't get dropped by the end of get_password?
I have tried the following, to no avail:
res.clone().as_str()res.to_owned().as_str()