Multithreading 如何设置圆弧和互斥变量的方框?

Multithreading 如何设置圆弧和互斥变量的方框?,multithreading,rust,mutex,reference-counting,Multithreading,Rust,Mutex,Reference Counting,下面的代码获取闭包中的一些变量,并返回包含该数据的结构 我无法返回包含该数据的结构,即使我将结构装箱并克隆变量;它们不可能超出这个范围。我曾想过使用回调闭包,但我真的不想这样做。有没有办法不用回电话就把它们拿出来 pub fn get(addr: &str) -> std::io::Result<Box<Response>> { use std::sync::{Arc, Mutex}; let mut crl = curl::easy::Ea

下面的代码获取闭包中的一些变量,并返回包含该数据的结构

我无法返回包含该数据的结构,即使我将结构装箱并克隆变量;它们不可能超出这个范围。我曾想过使用回调闭包,但我真的不想这样做。有没有办法不用回电话就把它们拿出来

pub fn get(addr: &str) -> std::io::Result<Box<Response>> {
    use std::sync::{Arc, Mutex};
    let mut crl = curl::easy::Easy::new();
    crl.url(format!("{}{}", API_ADDR, addr).as_str()).unwrap();

    // extract headers
    let headers: Vec<String> = Vec::with_capacity(10);
    let headers = Arc::new(Mutex::new(headers));
    {
        let headers = headers.clone();
        crl.header_function(move |h| {
            let mut headers = headers.lock().unwrap();
            (*headers).push(String::from_utf8_lossy(h).into_owned());
            true
        })
        .unwrap();
    }

    // extract body
    let body = Arc::new(Mutex::new(String::with_capacity(1024)));
    {
        let body = body.clone();
        crl.write_function(move |b| {
            let mut body = body.lock().unwrap();
            body.push_str(std::str::from_utf8(b).unwrap());
            Ok(b.len())
        })
        .unwrap();
    }
    crl.perform().unwrap();
    Ok(Box::new(Response {
        resp: body.lock().unwrap().clone(),
        headers: headers.lock().unwrap().clone(),
    }))
}
pub-fn-get(addr:&str)->std::io::Result{
使用std::sync::{Arc,Mutex};
让mut crl=curl::easy::easy::new();
url(格式!(“{}{}”,API_ADDR,ADDR).as_str()).unwrap();
//提取标题
let头:Vec=Vec::具有_容量(10);
让headers=Arc::new(Mutex::new(headers));
{
让headers=headers.clone();
crl.割台|U功能(移动| h|{
让mut headers=headers.lock().unwrap();
(*headers).push(字符串::from_utf8_lossy(h).into_owned());
真的
})
.unwrap();
}
//提取体
让body=Arc::new(Mutex::new(String::with_capacity(1024));
{
让body=body.clone();
crl.write|u函数(移动| b|{
让mut body=body.lock().unwrap();
body.push_str(std::str::from_utf8(b).unwrap());
好的(b.len())
})
.unwrap();
}
crl.perform().unwrap();
Ok(框::新建(响应{
resp:body.lock().unwrap().clone(),
headers:headers.lock().unwrap().clone(),
}))
}

关键错误似乎是:

错误[E0597]:`body`活得不够长
-->src/lib.rs:85:15
|
85 | resp:body.lock().unwrap().clone(),
|^^^^借来的价值活得不够长
...
89 | }
|-‘body’在这里还借着
|
=注意:范围中的值按与创建顺序相反的顺序删除
headers对象也是如此

我可以通过删除您的许多代码来获得简化的复制程序:

use std::sync::{Arc, Mutex};

pub struct Response {
    resp: String,
    headers: Vec<String>,
}

pub fn get(addr: &str) -> std::io::Result<Box<Response>> {
    let headers: Vec<String> = Vec::with_capacity(10);
    let headers = Arc::new(Mutex::new(headers));
    let body = Arc::new(Mutex::new(String::with_capacity(1024)));
    Ok(Box::new(Response {
        resp: body.lock().unwrap().clone(),
        headers: headers.lock().unwrap().clone(),
    }))
}

从中给出的更充分的解释中,我发现你可以这样写

return Ok(Box::new(Response {
    resp: body.lock().unwrap().clone(),
    headers: headers.lock().unwrap().clone(),
}));

i、 e.添加一个显式
返回
和一个尾随分号。虽然我有一种感觉clippy可能会说它的风格不好

这很好地解决了这个问题,但我不明白,因为它们是克隆的,它与tmp变量的生存期有什么关系我不是100%确定,我要求对一个大大减少的情况进行更全面的解释:。
return Ok(Box::new(Response {
    resp: body.lock().unwrap().clone(),
    headers: headers.lock().unwrap().clone(),
}));