pub struct Command { /* private fields */ }Expand description
A process builder, providing control over how a new process should be spawned.
Implementations§
Source§impl Command
impl Command
Sourcepub fn new(command: impl Into<OsString>) -> Self
pub fn new(command: impl Into<OsString>) -> Self
Create a new Command, with the following default configuration:
- Do not Inherit handles of the calling process.
- Inherit the current drive and directory of the calling process.
- Inherit the environment of the calling process.
Builder methods are provided to change these defaults and otherwise configure the process.
§Examples
use CreateProcessW::Command;
Command::new("notepad.exe")
.spawn()
.expect("notepad failed to start");Equivalent to the lpCommandLine parameter of the
CreateProcessW function.
Sourcepub fn inherit_handles(&mut self, inherit: bool) -> &mut Self
pub fn inherit_handles(&mut self, inherit: bool) -> &mut Self
Enable/disable handle inheritance.
When true, two things happen:
- Each inheritable handle in the calling process is inherited by the
new process (maps to
bInheritHandles). - The process and thread handles returned in
Childare themselves marked as inheritable, so future child processes can inherit them too (maps tobInheritHandleonlpProcessAttributes/lpThreadAttributes).
When false, neither happens.
Equivalent to the bInheritHandles parameter of the
CreateProcessW function.
Sourcepub fn current_dir(&mut self, dir: impl Into<PathBuf>) -> &mut Self
pub fn current_dir(&mut self, dir: impl Into<PathBuf>) -> &mut Self
Sets the working directory for the child process.
It’s the full path to the current directory for the process. Note that you can use a raw string to avoid error when copy-pasting the path.
§Examples
use CreateProcessW::Command;
let check = Command::new("cargo.exe check")
.current_dir(r"C:\Users\<user>\repos\<repo_name>")
.status()
.expect("cargo check command failed");Equivalent to the lpCurrentDirectory parameter of the
CreateProcessW function.
Sourcepub fn env<K, V>(&mut self, key: K, val: V) -> &mut Self
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Self
Inserts or updates an environment variable mapping.
When inheriting the parent’s environment (the default), the last call
to env for a given key wins. Earlier calls with the same key are
overridden.
A key should not contain ASCII = or a NUL byte.
§Examples
use CreateProcessW::Command;
Command::new("cmd.exe /c echo %MY_VAR%")
.env("MY_VAR", "hello")
.spawn()
.expect("failed to execute process");Sourcepub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
Adds or updates multiple environment variable mappings.
Works like repeated calls to env. The last
occurrence of a duplicate key wins.
Sourcepub fn env_remove<K>(&mut self, key: K) -> &mut Self
pub fn env_remove<K>(&mut self, key: K) -> &mut Self
Removes an environment variable from the inherited environment.
If env_clear is called before this method,
removal is a no-op at that point (the child already starts with an
empty environment). If env_clear is called
after this method, the removal is erased along with all other
environment configuration.
Note: The last operation on a key wins. Calling env_remove after
env for the same key removes it and calling
env after env_remove sets it.
§Examples
use CreateProcessW::Command;
Command::new("cmd.exe /c set")
.env_remove("PATH")
.spawn()
.expect("failed to execute process");Sourcepub fn env_clear(&mut self) -> &mut Self
pub fn env_clear(&mut self) -> &mut Self
Clears the entire environment map for the child process.
The child will not inherit any environment variables from the
parent process. Only variables added with env or
envs will be present.
§Examples
use CreateProcessW::Command;
Command::new("cmd.exe /c set")
.env_clear()
.env("MY_VAR", "hello")
.spawn()
.expect("failed to execute process");Sourcepub fn spawn(&mut self) -> Result<Child, Error>
pub fn spawn(&mut self) -> Result<Child, Error>
Executes the command as a child process, returning a handle to it.
§Examples
use CreateProcessW::Command;
Command::new("notepad.exe")
.spawn()
.expect("notepad failed to start");Sourcepub fn status(&mut self) -> Result<ExitStatus, Error>
pub fn status(&mut self) -> Result<ExitStatus, Error>
Executes a command as a child process, waiting for it to finish and collecting its status.
§Examples
use CreateProcessW::Command;
let status = Command::new("notepad.exe")
.status()
.expect("failed to execute process");
println!("process finished with: {}", status.code());
assert!(status.success());