-3

I am trying to execute the following code in Rust:

use std::io::Command;

fn main() {
    let the_output = Command::new("ruby").arg(["-e", "puts 'raja'", "x"]).output()
}

But it throws this error:

error[E0432]: unresolved import `std::io::Command`
 --> src\main.rs:1:5
  |
1 | use std::io::Command;
  |     ^^^^^^^^^^^^^^^^ no `Command` in `io`

Can someone guide me how to import this use std::io::Command; into my program?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Rajagopalan
  • 5,465
  • 2
  • 11
  • 29

1 Answers1

1

There isn't a std::io::Command. You were probably thinking of std::process::Command.

SCappella
  • 9,534
  • 1
  • 26
  • 35
  • 1
    Ah! Okay, thanks. Hi actually My requirement is, I wanted to run a command via `Command::new` For an example, typing `dir` in the command prompt will list out all the directories. How can I execute that command via this? If I type in my command prompt `ruby -e "puts 'raja'"` it would print `raja`, Now I wanted to execute the same line through `Command.new` and when I execute that line it has to print 'raja', how do I that? – Rajagopalan Apr 13 '19 at 06:30
  • For how to use `std::process::Command`, I recommend reading the documentation I linked. [This answer](https://stackoverflow.com/a/25574952/4639273) may also be enlightening. To print whatever that command sent to `stdout` and `stderr`, you can use the `stdout` and `stderr` fields on `the_output`. Just note that this data is kept as a `Vec`, so you'll have to convert it using something like `String::from_utf8`. If you're still confused, try asking a new question. – SCappella Apr 13 '19 at 06:57