Struct Command

Source
pub struct Command { /* private fields */ }
Expand description

Build a command to execute. This has an API which is deliberately similar to std::process::Command.

let err = exec::Command::new("echo")
    .arg("hello")
    .arg("world")
    .exec();
println!("Error: {}", err);

If the exec function succeeds, it will never return.

Implementations§

Source§

impl Command

Source

pub fn new<S: AsRef<OsStr>>(program: S) -> Command

Create a new command builder, specifying the program to run. The program will be searched for using the usual rules for PATH.

Examples found in repository?
examples/exec.rs (line 15)
6fn main() {
7    let argv: Vec<String> = env::args().skip(1).collect();
8    if argv.len() < 1 {
9        println!("Must specify command to execute");
10        process::exit(1);
11    }
12
13    // Exec the specified program.  If all goes well, this will never
14    // return.  If it does return, it will always retun an error.
15    let err = exec::Command::new(&argv[0]).args(&argv).exec();
16    println!("Error: {}", err);
17    process::exit(1);
18}
Source

pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command

Add an argument to the command builder. This can be chained.

Source

pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command

Add multiple arguments to the command builder. This can be chained.

let err = exec::Command::new("echo")
    .args(&["hello", "world"])
    .exec();
println!("Error: {}", err);
Examples found in repository?
examples/exec.rs (line 15)
6fn main() {
7    let argv: Vec<String> = env::args().skip(1).collect();
8    if argv.len() < 1 {
9        println!("Must specify command to execute");
10        process::exit(1);
11    }
12
13    // Exec the specified program.  If all goes well, this will never
14    // return.  If it does return, it will always retun an error.
15    let err = exec::Command::new(&argv[0]).args(&argv).exec();
16    println!("Error: {}", err);
17    process::exit(1);
18}
Source

pub fn exec(&mut self) -> Error

Execute the command we built. If this function succeeds, it will never return.

Examples found in repository?
examples/exec.rs (line 15)
6fn main() {
7    let argv: Vec<String> = env::args().skip(1).collect();
8    if argv.len() < 1 {
9        println!("Must specify command to execute");
10        process::exit(1);
11    }
12
13    // Exec the specified program.  If all goes well, this will never
14    // return.  If it does return, it will always retun an error.
15    let err = exec::Command::new(&argv[0]).args(&argv).exec();
16    println!("Error: {}", err);
17    process::exit(1);
18}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.