Is there a way to invoke a system command, like ls
or fuser
in Rust? How about capturing its output?
相关问题
- Share Arc between closures
- Function references: expected bound lifetime param
- Pattern matching on slices
- How can I iteratively call Sha256::digest, passing
- Destructure a vector into variables and give away
相关文章
- How can I convert a f64 to f32 and get the closest
- What is a good way of cleaning up after a unit tes
- How can I unpack (destructure) elements from a vec
- How to import macros in Rust?
- How to get struct field names in Rust? [duplicate]
- Confusion between [T] and &[T]
- How do I initialize an opaque C struct when using
- What's the most idiomatic way to test two Opti
std::process::Command
allows for that.There are multiple ways to spawn a child process and execute an arbitrary command on the machine:
spawn
— runs the program and returns a value with detailsoutput
— runs the program and returns the outputstatus
— runs the program and returns the exit codeOne simple example from the docs:
a very clear example from the docs:
It is indeed possible! The relevant module is
std::run
.ProcessOptions
’ standard file descriptors default toNone
(create a new pipe), so you can just useprocess.output()
(for example) to read from its output.If you want to run the command and get all its output after it’s done, there’s
wait_with_output
for that.Process::new
, as of yesterday, returns anOption<Process>
instead of aProcess
, by the way.