rustcommand

Existing command errors when invoked from Rust


I am new to Rust and I was trying to run the Windows "dir" command via Rust. However, when I do run the program, it outputs an Err and says the program is not found.

My main function is,

// gsync app
use std::process::Command;

fn main() {
    let res = Command::new("dir").output();
    println!("{:?}", res);
}

The output is,

   Compiling note v0.1.0 (C:\Users\Kavin\ultron\note)
    Finished dev [unoptimized + debuginfo] target(s) in 0.38s
     Running `target\debug\note.exe`
Err(Error { kind: NotFound, message: "program not found" })

what am I doing wrong?! Thank you for helping out.


Solution

  • Since there is no official answer to this question, I've decided to "answer" it myself by explaining @Jmb 's comment. As per the official Rust book, the std::process::Command; does the following,

    Constructs a new Command for launching the program at path program, with the following default configuration:
    1. No arguments to the program
    2. Inherit the current process’s environment
    3. Inherit the current process’s working directory
    4. Inherit stdin/stdout/stderr for spawn or status, but create pipes for output
    

    Since dir is not a program is Windows 11 (the platform I was using), it was not giving the expected output.