5
我這裏有這樣的代碼:(Playground link)通用FN,渠道和線程產卵
use std::thread;
use std::sync::mpsc::channel;
fn run<T: Send>(task: fn() -> T) -> T {
let (tx, rx) = channel();
thread::spawn(move || {
tx.send(task());
});
rx.recv().unwrap()
}
fn main() {
let task = || 1 + 2;
let result = run(task);
println!("{}", result);
}
但我得到了一生的錯誤,我想不通。
<anon>:6:5: 6:18 error: the parameter type `T` may not live long enough [E0310]
<anon>:6 thread::spawn(move || {
^~~~~~~~~~~~~
<anon>:6:5: 6:18 help: consider adding an explicit lifetime bound `T: 'static`...
<anon>:6:5: 6:18 note: ...so that captured variable `tx` does not outlive the enclosing closure
<anon>:6 thread::spawn(move || {
^~~~~~~~~~~~~
<anon>:15:22: 15:26 error: mismatched types:
expected `fn() -> _`,
found `[closure <anon>:13:16: 13:24]`
(expected fn pointer,
found closure) [E0308]
<anon>:15 let result = run(task);
^~~~
有什麼建議嗎?謝謝!
這是偉大的,謝謝!我在這裏設法解決了封閉版本:http://is.gd/8UwpjT - 添加''static'總是感覺像一個骯髒的選擇。文檔說,這意味着該項目將持續該計劃的生命。這是不好的做法嗎?什麼時候可以使用靜態生命週期? – jocull
經過一番實驗,我*認爲*我明白了...... http://is.gd/8KWutk「靜態」意味着給定的特質是靜態的 - 不是傳入的項目!這更有意義。 – jocull