1
魯斯特抱怨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
}
您試圖返回對局部變量的引用;局部變量不足以返回一個引用_ because_它是本地的... – ildjarn
基本上是[有什麼方法可以返回對函數中創建的變量的引用?]的副本(http://stackoverflow.com/q/32682876/155423) – Shepmaster