How to execute a system command and capture its output in Rust?
Hello, colleagues!\n\nI started learning Rust and am writing a small automation tool. I need to execute an external system command (e.g., `ls -la` or `git status`) and read its output (stdout and stderr) directly in the program, so I can then process the lines.\n\nI looked at the documentation for the `std::process::Command` module, but I can't figure out how to properly capture the output. Here's what I tried:\n\n```rust\nuse std::process::Command;\n\nfn main() {\n let output = Command::new("ls")\n .arg("-la")\n .output()\n .expect("Failed to execute command");\n\n println!("stdout: {}", output.stdout);\n println!("stderr: {}", output.stderr);\n}\n```\n\nBut the console prints something like `stdout: [115, 111, 109, 101, 32, 116, 101, 120, 116]` — these are bytes, not a string! How
No answers yet.
Log in to leave an answer