Run external command in another directory



examples/external/chdir-for-execution/src/main.rs
use std::process::Command;

fn main() {
    let result = Command::new("pwd")
        .output()
        .expect("pwd command failed to start");
    print!("{}", std::str::from_utf8(&result.stdout).unwrap());

    let result = Command::new("pwd")
        .current_dir("src")
        .output()
        .expect("pwd command failed to start");
    print!("{}", std::str::from_utf8(&result.stdout).unwrap());

    let result = Command::new("pwd")
        .current_dir("/etc/")
        .output()
        .expect("pwd command failed to start");
    print!("{}", std::str::from_utf8(&result.stdout).unwrap());

    println!("{}", std::env::current_dir().unwrap().display());
}

/home/gabor/work/slides/rust/examples/external/chdir-for-execution
/home/gabor/work/slides/rust/examples/external/chdir-for-execution/src
/etc
/home/gabor/work/slides/rust/examples/external/chdir-for-execution