mirror of https://github.com/sunface/rust-course
				
				
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							81 lines
						
					
					
						
							2.3 KiB
						
					
					
				
			
		
		
	
	
							81 lines
						
					
					
						
							2.3 KiB
						
					
					
				关于调用os的命令
 | 
						|
 | 
						|
```rust
 | 
						|
use std::process::Command;
 | 
						|
 | 
						|
fn main() {
 | 
						|
    let output = Command::new("rustc")
 | 
						|
        .arg("--versn")
 | 
						|
        .output().unwrap_or_else(|e| {
 | 
						|
            panic!("failed to execute process: {}", e)
 | 
						|
    });
 | 
						|
 | 
						|
    if output.status.success() {
 | 
						|
        let s = String::from_utf8_lossy(&output.stdout);
 | 
						|
 | 
						|
        print!("rustc succeeded and stdout was:\n{}", s);
 | 
						|
    } else {
 | 
						|
        let s = String::from_utf8_lossy(&output.stderr);
 | 
						|
 | 
						|
        print!("rustc failed and stderr was:\n{}", s);
 | 
						|
    }
 | 
						|
}
 | 
						|
```
 | 
						|
 | 
						|
## Pipes
 | 
						|
The std::Child struct represents a running child process, and exposes the stdin, stdout and stderr handles for interaction with the underlying process via pipes.
 | 
						|
 | 
						|
```rust
 | 
						|
use std::io::prelude::*;
 | 
						|
use std::process::{Command, Stdio};
 | 
						|
 | 
						|
static PANGRAM: &'static str =
 | 
						|
"the quick brown fox jumped over the lazy dog\n";
 | 
						|
 | 
						|
fn main() {
 | 
						|
    // Spawn the `wc` command
 | 
						|
    let process = match Command::new("wc")
 | 
						|
                                .stdin(Stdio::piped())
 | 
						|
                                .stdout(Stdio::piped())
 | 
						|
                                .spawn() {
 | 
						|
        Err(why) => panic!("couldn't spawn wc: {}", why),
 | 
						|
        Ok(process) => process,
 | 
						|
    };
 | 
						|
 | 
						|
    // Write a string to the `stdin` of `wc`.
 | 
						|
    //
 | 
						|
    // `stdin` has type `Option<ChildStdin>`, but since we know this instance
 | 
						|
    // must have one, we can directly `unwrap` it.
 | 
						|
    match process.stdin.unwrap().write_all(PANGRAM.as_bytes()) {
 | 
						|
        Err(why) => panic!("couldn't write to wc stdin: {}", why),
 | 
						|
        Ok(_) => println!("sent pangram to wc"),
 | 
						|
    }
 | 
						|
 | 
						|
    // Because `stdin` does not live after the above calls, it is `drop`ed,
 | 
						|
    // and the pipe is closed.
 | 
						|
    //
 | 
						|
    // This is very important, otherwise `wc` wouldn't start processing the
 | 
						|
    // input we just sent.
 | 
						|
 | 
						|
    // The `stdout` field also has type `Option<ChildStdout>` so must be unwrapped.
 | 
						|
    let mut s = String::new();
 | 
						|
    match process.stdout.unwrap().read_to_string(&mut s) {
 | 
						|
        Err(why) => panic!("couldn't read wc stdout: {}", why),
 | 
						|
        Ok(_) => print!("wc responded with:\n{}", s),
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
```
 | 
						|
 | 
						|
 | 
						|
## 调用命令,使用用户的输入作为参数
 | 
						|
 | 
						|
```rust
 | 
						|
let cmd = Command::new("rev")
 | 
						|
    .stdin(Stdio::inherit())
 | 
						|
    .stdout(Stdio::inherit())
 | 
						|
    .output()?;
 | 
						|
```
 | 
						|
 | 
						|
https://www.reddit.com/r/learnrust/comments/r5wwkz/what_the_relationship_between_processstdio_and/
 |