Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 无法为生存期参数克隆特征对象推断适当的生存期_Rust - Fatal编程技术网

Rust 无法为生存期参数克隆特征对象推断适当的生存期

Rust 无法为生存期参数克隆特征对象推断适当的生存期,rust,Rust,这个问题的重复似乎不能解决我的问题。以下代码给出了错误: use std::collections::HashMap; use std::thread; pub trait Spider : Sync + Send { fn add_request_headers(&self, headers: &mut Vec<String>); } pub struct Google {} impl Spider for Google { fn add_r

这个问题的重复似乎不能解决我的问题。以下代码给出了错误:

use std::collections::HashMap;
use std::thread;


pub trait Spider : Sync + Send {
    fn add_request_headers(&self, headers: &mut Vec<String>);
}

pub struct Google {}

impl Spider for Google {
    fn add_request_headers(&self, headers: &mut Vec<String>) {
        headers.push("Hello".to_string())
    }
}

fn parallel_get(spiders: &HashMap<String, Box<Spider>>) -> std::thread::JoinHandle<()> {
    let thread_spiders = spiders.clone();
    thread::spawn(move || {
        let headers = &mut vec![];
        let spider = thread_spiders.get("Google").unwrap();
        spider.add_request_headers(headers);
    })
}

fn main() {
    let spiders = HashMap::new();
    let spider = Box::new(Google{});
    spiders.insert("Google", spider);
}
使用std::collections::HashMap;
使用std::线程;
发布特征蜘蛛:同步+发送{
fn添加请求头(&self,头:&mut-Vec);
}
发布结构Google{}
谷歌的impl蜘蛛{
fn添加请求头(&self,头:&mut-Vec){
headers.push(“Hello.to_string())
}
}
fn parallel_get(spider:&HashMap)->std::thread::JoinHandle{
让线程_spider=spider.clone();
线程::生成(移动| |{
let headers=&mut vec![];
让spider=thread_spider.get(“谷歌”).unwrap();
spider.add_request_头(headers);
})
}
fn main(){
让spider=HashMap::new();
让spider=Box::new(Google{});
spider.insert(“谷歌”,spider);
}
在操场上跑步

我得到:

rustc 1.14.0(e8a012324 2016-12-16)
错误[E0495]:由于需求冲突,无法为生存期参数“a”推断适当的生存期
--> :18:34
|
18 |让线程_spider=spider.clone();
|                                  ^^^^^
|
注意:首先,生命周期不能超过17:87在块上定义的匿名生命周期#1。。。
--> :17:88
|
17 | fn parallel_get(spider:&HashMap)->std::thread::JoinHandle{
|                                                                                        ^

注意:…因此类型是兼容的(预期的&&std::collections::HashMap,发现的&&std::collections::HashMap在这种情况下,错误消息非常混乱,但请注意这里有一个双符号AND:
(预期的&&std::collections::HashMap,发现的&&std::collections::HashMap请链接到您已阅读并确定为没有帮助的副本,否则我们很可能在回答您的问题时重复这些副本,那么没有人得到帮助。例如,似乎与您的实际代码非常匹配。
error[E0277]: the trait bound `Spider: std::clone::Clone` is not satisfied
error[E0277]: the trait bound `Spider: std::marker::Sized` is not satisfied
  --> error_orig.rs:19:26
   |
19 |     let thread_spiders = Clone::clone(spiders);
   |                          ^^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `Spider`
   |
   = note: `Spider` does not have a constant size known at compile-time
   = note: required because of the requirements on the impl of `std::clone::Clone` for `Box<Spider>`
   = note: required because of the requirements on the impl of `std::clone::Clone` for `std::collections::HashMap<std::string::String, Box<Spider>>`
   = note: required by `std::clone::Clone::clone`
pub trait Spider: Sync + Send {
    fn add_request_headers(&self, headers: &mut Vec<String>);
    fn clone_into_box(&self) -> Box<Spider>;
}

impl Clone for Box<Spider> {
    fn clone(&self) -> Self {
        self.clone_into_box()
    }
}

#[derive(Clone)]
pub struct Google {}

impl Spider for Google {
    fn add_request_headers(&self, headers: &mut Vec<String>) {
        headers.push("Hello".to_string())
    }

    fn clone_into_box(&self) -> Box<Spider> {
        Box::new(self.clone())
    }
}