Execing Processes in Rust
Here’s the translation of the Go code to Rust, with explanations in Markdown format suitable for Hugo:
In the previous example, we looked at spawning external processes. We do this when we need an external process accessible to a running Rust process. Sometimes we just want to completely replace the current Rust process with another (perhaps non-Rust) one. To do this, we’ll use Rust’s implementation of the classic exec
function.
use std::process::Command;
use std::os::unix::process::CommandExt;
fn main() {
// For our example we'll exec `ls`. Rust doesn't require an
// absolute path to the binary we want to execute, so we can
// use `ls` directly.
let mut cmd = Command::new("ls");
// We'll give `ls` a few common arguments. Note that we add
// these arguments separately.
cmd.arg("-a").arg("-l").arg("-h");
// In Rust, we don't need to explicitly set the environment
// variables. The child process will inherit the environment
// of the parent process by default.
// Here's the actual `exec` call. If this call is successful,
// the execution of our process will end here and be replaced
// by the `/bin/ls -a -l -h` process. If there is an error,
// we'll get a return value.
let error = cmd.exec();
// If we reach here, it means the exec call failed
eprintln!("Failed to execute process: {:?}", error);
}
When we run our program it is replaced by ls
.
$ cargo run
total 16
drwxr-xr-x 4 user 136B Oct 3 16:29 .
drwxr-xr-x 91 user 3.0K Oct 3 12:50 ..
-rw-r--r-- 1 user 1.3K Oct 3 16:28 main.rs
Note that Rust, like many other languages, does not offer a classic Unix fork
function. Usually this isn’t an issue though, since spawning processes and exec’ing processes covers most use cases for fork
. For more complex scenarios, Rust provides the std::process
module which offers fine-grained control over process creation and management.