Running an external process in Rust -
Running an external process in Rust -
i saw question how invoke scheme command in rust program? seems has changed. how run external process in rust now?
have tried std::io::process::command
?
you seek like.
let process = match std::io::process::command::new("ls") .args(&["-l", "/home/hduser"]) .spawn() { ok(process) => process, err(err) => panic!("running process error: {}", err), }; allow output = match process.wait_with_output() { ok(output) => output, err(err) => panic!("retrieving output error: {}", err), }; allow stdout = match std::string::string::from_utf8(output.output) { ok(stdout) => stdout, err(err) => panic!("translating output error: {}", err), }; print!("{}", stdout);
you don't have spawn process, rust great at, why not. command::new
returns option
, wait_with_output()
returns ioresult
, , from_utf8
returns result
, used match look disclose results, utilize .ok().expect("some descriptive text error")
instead of match expressions.
example without spawn , match expressions:
let process = std::io::process::command::new("pwd") .output() .ok() .expect("failed execute"); allow out = std::string::string::from_utf8(process.output) .ok() .expect("failed read")); println!("{}", out);
rust
Comments
Post a Comment