18.5.1 Pipes

Process 구조체는 실행 중인 하위 프로세스를 나타내고, stdin, stdout 그리고 stderr같은 핸들을 공개하여 pipes를 통해 상호작용 한다.

use std::error::Error;
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() {
// `wc` .
let process = match Command::new("wc")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn() {
Err(why) => panic!("couldn't spawn wc: {}", why.description()),
Ok(process) => process,
};
// `wc` `stdin` string .
//
// `stdin` `Option<ChildStdin>`
// , `unwrap` .
match process.stdin.unwrap().write_all(PANGRAM.as_bytes()) {
Err(why) => panic!("couldn't write to wc stdin: {}",
why.description()),
Ok(_) => println!("sent pangram to wc"),
}
// `stdin` , `drop`,
// pipe .
//
// , `wc`
// .
let mut s = String::new();
match process.stdout.unwrap().read_to_string(&mut s) {
Err(why) => panic!("couldn't read wc stdout: {}",
why.description()),
Ok(_) => print!("wc responded with:\n{}", s),
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

results matching ""

    No results matching ""