Skip to main content

Crate command_extra

Crate command_extra 

Source
Expand description

Builder-style methods for Command that take self and return Self.

std’s own builder methods return &mut Command. They chain, but the chain cannot produce a value, so a command that needs several settings has to be built over a mutable binding and handed back separately. These methods return the command itself, which puts the whole construction in expression position: it can be returned, bound, stored in a field, or folded over.

fn lister(dir: &Path) -> Command {
    Command::new("ls")
        .with_current_dir(dir)
        .with_args(["-l", "-a"])
        .with_env("LANG", "C")
}

The same function written against std cannot end in its chain, because the chain has type &mut Command:

fn lister(dir: &Path) -> Command {
    let mut command = Command::new("ls");
    command
        .current_dir(dir)
        .args(["-l", "-a"])
        .env("LANG", "C");
    command
}

Traits§

CommandExtra
Builder-style methods for Command that take self and return Self.