简体   繁体   English

如何解决这个终身问题?

[英]How do I fix this lifetime issue?

In the code below, the string does not live long enough in the Into<Body<'a>> implementation for RequestParameters<'a> . 在下面的代码中,该stringRequestParameters<'a>Into<Body<'a>>实现中寿命不足。 I understand why, since string comes into scope inside of into and is no longer in scope after the method finishes, but Body<'a> will keep a reference to it. 我明白为什么,因为string进入活动范围内into是范围中不再方法完成后,但Body<'a>将保持对它的引用。

At least, that's why I think string doesn't last long enough. 至少,这就是为什么我认为string不能持续足够长的时间的原因。

What I don't understand is how to structure this code to fix string 's lifetime. 我不明白的是如何构造此代码来修复string的生命周期。

The goal of this code is to make a HashMap (of "a" to "b" for example) into a string for the body of a POST request ( "?a=b" , for example). 此代码的目标是将HashMap(例如,从"a""b" )转换为POST请求正文的字符串(例如, "?a=b" )。 If there is a better way to do this, please let me know, but what I would benefit a lot from is an understanding of how to fix this lifetime issue. 如果有更好的方法可以解决此问题,请告诉我,但是我将从中受益匪浅。

If I'm wrong about why string doesn't live long enough, please let me know, as well. 如果我对string寿命不足的原因有误,也请让我知道。 I'm still trying to come to grips with the lifetime system in Rust, so figuring this out will help me a ton. 我仍在努力掌握Rust的生命周期系统,因此弄清楚这一点将对我有很大帮助。

struct RequestParameters<'a> {
    map: HashMap<&'a str, &'a str>,
}

impl<'a> From<HashMap<&'a str, &'a str>> for RequestParameters<'a> {
    fn from(map: HashMap<&'a str, &'a str>) -> RequestParameters<'a> {
        RequestParameters { map: map }
    }
}

impl<'a> Into<Body<'a>> for RequestParameters<'a> {
    fn into(self) -> Body<'a> {
        let string = String::from("?") +
                     &self.map
            .iter()
            .map(|entry| format!("&{}={}", entry.0, entry.1))
            .collect::<String>()[1..];
        (&string).into()
    }
}

fn main() {
    let mut parameters = HashMap::new();
    parameters.insert("a", "b");
    let client = Client::new();
    client.post("https://google.com")
        .body(RequestParameters::from(parameters));
}

As Vladimir's link points out, this is actually impossible. 正如弗拉基米尔(Vladimir)的链接所指出的那样,这实际上是不可能的。 I changed my code to reflect this knowledge, and it now compiles. 我更改了代码以反映此知识,然后将其编译。

struct RequestParameters<'a> {
    map: HashMap<&'a str, &'a str>,
}

impl<'a> From<HashMap<&'a str, &'a str>> for RequestParameters<'a> {
    fn from(map: HashMap<&'a str, &'a str>) -> RequestParameters<'a> {
        RequestParameters { map: map }
    }
}

impl<'a> RequestParameters<'a> {
    fn to_string(self) -> String {
        String::from("?") +
        &self.map.iter().map(|entry| format!("&{}={}", entry.0, entry.1)).collect::<String>()[1..]
    }
}

fn main() {
    let mut parameters = HashMap::new();
    parameters.insert("a", "b");
    let string_parameters = RequestParameters::from(parameters).to_string();
    let client = Client::new();
    client.post("https://google.com")
        .body(&string_parameters);
}

By creating the String before the Client is made I can borrow it with a longer lifetime than the Client , which fixes my issues. 通过在创建Client之前创建String ,我可以借用它的寿命比Client更长的寿命,这可以解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM