我想在进程运行时运行一个进程并处理它的标准输出,但我不知道该怎么做。
fn execute_command(&mut self){
let (exec_cmd, cmd_args) = self.command_as_arguments.split_at(1);
let mut cmd_output = Command::new(exec_cmd[0]);
// std::process::Command = cmd_output
for arg in cmd_args{
cmd_output.arg(arg);
}
// let std_out_handle = cmd_output.stdout(Stdio::null());
let std_out_handle = cmd_output.stdout(Stdio::piped());
// let std_out_handle = cmd_output.stdout(Stdio::inherit());
let ok_cprocess_handle = std_out_handle.spawn().unwrap();
//process::Child = ok_cprocess_handle
let mut ok_cprocess_handle_stdout = ok_cprocess_handle.stdout.unwrap();
// println!("CHILD STDOUT={:?}",ok_cprocess_handle_stdout);
let mut output_string = String::new();
ok_cprocess_handle_stdout.read_to_string(&mut output_string);
println!("CHILD STRING STDOUT={:?}",output_string);
let mut output_vec = Vec::new();
ok_cprocess_handle_stdout.read_to_end(&mut output_vec);
println!("CHILD VEC STDOUT={:?}",output_vec);
}
我编辑并减少了代码,但是当我运行程序时,如果我尝试读取输出,我仍然会得到一个空字符串:ok_cprocess_handle_stdout.read_to_string(&mut output_string);
println!("CHILD STRING STDOUT={:?}",output_string);
我期待在那时阅读一些输出,但我错过了什么?