cannot move out of `*response` which is behind a shared reference with Rust while parsing a json response

huangapple go评论92阅读模式
英文:

cannot move out of `*response` which is behind a shared reference with Rust while parsing a json response

问题

抱歉,我不能提供代码的直接翻译,因为代码部分包含特定的编程术语和语法,难以进行简单的机器翻译。如果您需要关于代码的帮助,请提出具体的问题,我将尽力提供解答。

英文:

I'm a newbie that is trying to make a simple email parser, but I'm facing the usual newcomers' problems with the borrowing stuff in Rust.

I'm trying to make compile the following code that wants to parse a json response with reqwest into my struct MessagesListResponse and then I just want to return a Vec of strings only containing the mapped ids:

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct MessagesListResponse{
    messages: Vec<MessageId>
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct MessageId{
    id: String,
    thread_id: String
}


#[async_trait]
impl MessageGetter for GmailMessageGetter{

    async fn get_message_ids(&self) -> Vec<String> {
        

        let url = "https://gmail.googleapis.com/gmail/v1/users/me/messages?q=from%3Awebank%40webank.it%20subject%3Aautorizzato%20pagamento".to_owned();
        
        dbg!(&url);
        let response = &self.client.get(url).send().await.unwrap();

        _ = &response.error_for_status_ref().unwrap();
        let msg_list_response = response.json::<MessagesListResponse>().await.unwrap();
        msg_list_response.messages.into_iter().map(|message_id| message_id.id).collect()
    }
    
}

unfortunately I keep getting the error

cannot move out of `*response` which is behind a shared reference

at the line

let msg_list_response = response.json::<MessagesListResponse>().await.unwrap();

but I'm not understanding how to make it work because the response type reqwest::async_impl::response is not cloneable and the response.json method consumes the response. Can I get some help? Thanks a lot.

答案1

得分: 1

以下是翻译后的代码部分:

这里有一个不必要的引用。

let response = client.get(url).send().await.unwrap();
这里的赋值和引用是不必要的,没有实际作用。

_ = response.error_for_status_ref().unwrap();
URL 末尾的 `.to_owned()` 也是不必要的。你应该移除它。

这些是唯一的问题,但还有另一种方式将其写成一个大的方法链。

client
    .get(url)
    .send()
    .await
    .unwrap()
    .error_for_status()
    .unwrap()
    .json::<MessagesListResponse>()
    .await
    .unwrap()
    .messages
    .into_iter()
    .map(|message_id| message_id.id)
    .collect()
英文:

There's an unnecessary reference here.

let response = &amp;client.get(url).send().await.unwrap();

Remove it, and your code works.

let response = client.get(url).send().await.unwrap();

The assignment and reference here are unnecessary and do nothing.

_ = &amp;response.error_for_status_ref().unwrap();

You can remove them.

response.error_for_status_ref().unwrap();

The .to_owned() at the end of the URL is also unnecessary. You should remove it.

Those are the only issues, but another way to write this is as one big method chain.

client
    .get(url)
    .send()
    .await
    .unwrap()
    .error_for_status()
    .unwrap()
    .json::&lt;MessagesListResponse&gt;()
    .await
    .unwrap()
    .messages
    .into_iter()
    .map(|message_id| message_id.id)
    .collect()

huangapple
  • 本文由 发表于 2023年5月29日 19:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定