正如標題已經說過的,我試圖將一個&[&str]
移動到一個線程中。嗯,其實,下面的代碼工作,但我有兩個問題是:將一個&[&str]移動到一個線程
let args2: Vec<_> = args.iter().map(|arg| { arg.to_string() }).collect();
似乎有點冗長的&[&str]
轉換爲Vec<String>
。這可以做到「更好」嗎?- 如果我理解正確,字符串會被複制兩次:第一個是
let cmd2
和let args2
語句;然後通過在move
關閉內移動它們。它是否正確?如果是這樣,可以用一個副本來完成嗎?
我所知道的thread::scoped
,不過目前已被棄用。我也在編寫這個代碼來學習一些關於Rust的知識,所以關於「不可信」代碼的評論也是值得讚賞的。
use std::process::{Command,Output};
use std::thread;
use std::thread::JoinHandle;
pub struct Process {
joiner: JoinHandle<Output>,
}
impl Process {
pub fn new(cmd: &str, args: &[&str]) -> Process {
// Copy the strings for the thread
let cmd2 = cmd.to_string();
let args2: Vec<_> = args.iter().map(|arg| { arg.to_string() }).collect();
let child = thread::spawn(move || {
Command::new(cmd2).args(&args2[..]).output().unwrap_or_else(|e| {
panic!("Failed to execute process: {}", e)
})
});
Process { joiner: child }
}
}
謝謝你'String :: from'。對於第二點,我首先明確地將字符串複製到'cmd2'和'args2'中,但是當它們被移入閉包時,它們是不是也被物理地複製到了線程堆棧中呢? – Garogolun
@Garogolun那麼,矢量被複制,但這只是一個淺拷貝(即包含指針,長度和容量的結構)。矢量的內容不被複制。 – fjh
線程可以訪問向量的內容。但是,如果'Process :: new'返回,則矢量及其內容會被破壞。那麼這不是不安全嗎? – Garogolun