Is there anything left unclear? If so, where it is: in the explanation or in the documentation I've linked to? Last thing we're doing here is to unwrap the returned value: since the command may fail (for variety of reasons), output (and status) return Result, which can be expected to either return the inner value or panic with set message. If you don't need this and want to pipe the output to the shell from which the main program was launched, you can use status method instead, which will yield only the exit code. As stated in the docs, this method capture the program output into two Vec, for stdout and stderr correspondingly, so that the program can analyze it. Each element of this array is passed as an argument to the command result will be the same as if you've written cmd /C "echo hello" in the command interpreter itself (note the quotes - without them echo hello would be two arguments, not one). & and we're creating this slice by taking a reference to an array. In particular, this method can accept a slice containing string slices, i.e. The definition of this method looks a bit complicated, but in fact it is simple: it accepts anything which can be iterated, so that this iteration will yield something to be interpreted as OsStr. Then, we're passing some arguments to it through the args method on Command. some command which is interpreted by the shell itself and not as external program. Note that in most cases you don't need this - you can pass the program name to the Command::new directly invoking shell explicitly might be necessary if we're about to use the shell builtin, i.e. We're on Windows, so we continue for the first if branch.Ĭommand::new requires one argument, which is the command name. In this case, the macro will expand to true on Windows and to false in all other cases. If cfg!(target_os = "windows") means that we are referring to some compile-time configuration through cfg! macro.
0 Comments
Leave a Reply. |