Rust 抱怨它的get_string
寿命不够长。它似乎想在整个函数范围内保持活力,但我看不出这是怎么发生的。
error: `get_string` does not live long enough
--> src\lib.rs:7:23
|
7 | for value_pair in get_string.split('&') {
| ^^^^^^^^^^ does not live long enough
...
19 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the body at 3:59...
--> src\lib.rs:3:60
|
3 | fn parse_get(get_string: &str) -> HashMap<&str, Vec<&str>> {
| ^
use std::collections::HashMap;
fn parse_get(get_string: &str) -> HashMap<&str, Vec<&str>> {
let get_string = String::from(get_string);
let mut parameters: HashMap<&str, Vec<&str>> = HashMap::new();
for value_pair in get_string.split('&') {
let name = value_pair.split('=').nth(0).unwrap();
let value = value_pair.split('=').nth(1).unwrap();
if parameters.contains_key(name) {
parameters.get_mut(name).unwrap().push(value);
} else {
parameters.insert(name, vec![value]);
}
}
parameters
}