The following steps will walk you through the implementation:
- Create a file named sample_child_process.rs and open it in your text editor.
- Write the code header with the relevant information:
//-- #########################
//-- Task: To call a child process
//-- Author: Vigneshwer.D
//-- Version: 1.0.0
//-- Date: 19 March 17
//-- #########################
- Call the standard library:
// Call the standard library
use std::process::Command;
- Define the main function and declare the output variable, which is the Command implementation to execute the child process and get std:output:
// Main execution of the code
fn main() {
// Command to be executed
let output = Command::new("rustc")
.arg("--version")
.output().unwrap_or_else(|e| {
panic!("failed to execute process: {}", e)
});
- Print out the string value of s variable based on the output response using if...else statements:
// printing the output values
if output.status.success() {
let s = String::from_utf8_lossy(&output.stdout);
print!("rustc succeeded and stdout was:n{}", s);
}
else {
let s = String::from_utf8_lossy(&output.stderr);
print!("rustc failed and stderr was:n{}", s);
}
}
You will get the following output upon successful execution of the code: