此問題的重複似乎無法爲我解決問題。下面的代碼給我的錯誤:在操場上here無法推斷生命期參數克隆特徵對象的適當生命期
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);
}
運行。
我得到:
rustc 1.14.0 (e8a-12-16)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> <anon>:18:34
|
18 | let thread_spiders = spiders.clone();
| ^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the block at 17:87...
--> <anon>:17:88
|
17 | fn parallel_get(spiders: &HashMap<String, Box<Spider>>) -> std::thread::JoinHandle<()> {
| ^
note: ...so that types are compatible (expected &&std::collections::HashMap<std::string::String, Box<Spider>>, found &&std::collections::HashMap<std::string::String, Box<Spider + 'static>>)
--> <anon>:18:34
|
18 | let thread_spiders = spiders.clone();
| ^^^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[[email protected]<anon>:19:19: 23:6 thread_spiders:&std::collections::HashMap<std::string::String, Box<Spider>>]` will meet its required lifetime bounds
--> <anon>:19:5
|
19 | thread::spawn(move || {
| ^^^^^^^^^^^^^
我認爲它告訴我,因爲它需要'static
足夠長住它的線程不能自動推斷thread_spiders
壽命,但它不能活得長'a
這是輸入參數的生命週期。
問題是,我可以在parallel_get
中克隆其他對象,並且它們可以毫無問題地移入新線程。但由於某種原因thread_spiders
似乎絆倒了編譯器。如果我是正確的,它應該有一個'a
的生命週期,然後進入線程關閉。
我試過在parallel_get
中添加明確的生命週期參數,但一直未能得到任何工作。我怎樣才能讓這段代碼編譯?
請**鏈接到**您已閱讀並認定爲無幫助的副本,否則我們可能會在回答您的問題時重複這些副本,然後無人幫助。 – Shepmaster
例如,[如何克隆包含盒裝特質對象的HashMap?](http://stackoverflow.com/q/39120183/155423)似乎與您的實際代碼非常接近。 – Shepmaster