How to do it...

The following steps will walk you through the implementation:

  1. Create a file named sample_child_process.rs and open it in your text editor.
  2. 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
//-- #########################
  1. Call the standard library:
        // Call the standard library
use std::process::Command;
  1. 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)
});
  1. 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:



..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.186.109