home ~ projects ~ socials

Get The Text Output From An External Command In Rust

use std::process::Command;

fn main() {
    let args = vec!["-l"];
    let response = Command::new("ls")
        .args(args)
        .output()
        .expect("did not work");
    let text = String::from_utf8_lossy(&response.stdout);
    dbg!(text);
}

Notes

  • The default that comes back from output is a Vec-u8- that has to be turned into a string to output it if you want to see it as letters.
  • I wouldn't be surprised to find other ways to do this.
-- end of line --