Skip to content

Commit 53231e6

Browse files
committed
add chapter 07
1 parent 49879c5 commit 53231e6

File tree

3 files changed

+74
-51
lines changed

3 files changed

+74
-51
lines changed

asyncwait/src/runtimes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub fn futures_select() {
139139

140140
pub fn smol_zip() {
141141
smol::block_on(async {
142-
use smol::future::{try_zip, zip, FutureExt};
142+
use smol::future::{try_zip, zip};
143143

144144
let future1 = async { 1 };
145145
let future2 = async { 2 };

process/src/lib.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,75 @@ pub fn easy_process_example() {
5656
}
5757

5858
}
59+
60+
pub fn pipe() {
61+
// 创建两个子进程,一个作为生产者,一个作为消费者
62+
63+
// 生产者进程
64+
let producer = Command::new("echo")
65+
.arg("Hello, Rust!")
66+
.stdout(Stdio::piped())
67+
.spawn()
68+
.expect("Failed to start producer command");
69+
70+
// 消费者进程
71+
let consumer = Command::new("grep")
72+
.arg("Rust")
73+
.stdin(producer.stdout.unwrap())
74+
.output()
75+
.expect("Failed to start consumer command");
76+
77+
// 获取消费者的输出
78+
let output = String::from_utf8_lossy(&consumer.stdout);
79+
println!("Output: {:?}", output);
80+
}
81+
82+
pub fn spawn_a_process() {
83+
let output = Command::new("echo")
84+
.arg("Hello world")
85+
.output()
86+
.expect("Failed to execute command");
87+
88+
assert_eq!(b"Hello world\n", output.stdout.as_slice());
89+
}
90+
91+
pub fn process_io() {
92+
let echo_child = Command::new("echo")
93+
.arg("Oh no, a tpyo!")
94+
.stdout(Stdio::piped())
95+
.spawn()
96+
.expect("Failed to start echo process");
97+
98+
let echo_out = echo_child.stdout.expect("Failed to open echo stdout");
99+
100+
let sed_child = Command::new("sed")
101+
.arg("s/tpyo/typo/")
102+
.stdin(Stdio::from(echo_out))
103+
.stdout(Stdio::piped())
104+
.spawn()
105+
.expect("Failed to start sed process");
106+
107+
let output = sed_child.wait_with_output().expect("Failed to wait on sed");
108+
assert_eq!(b"Oh no, a typo!\n", output.stdout.as_slice());
109+
}
110+
111+
pub fn child() {
112+
let mut child = Command::new("/bin/cat")
113+
.arg("Cargo.toml")
114+
.spawn()
115+
.expect("failed to execute child");
116+
117+
let ecode = child.wait().expect("failed to wait on child");
118+
119+
assert!(ecode.success());
120+
}
121+
122+
pub fn kill() {
123+
let mut command = Command::new("yes");
124+
if let Ok(mut child) = command.spawn() {
125+
println!("Child's ID is {}", child.id());
126+
child.kill().expect("command wasn't running");
127+
} else {
128+
println!("yes command didn't start");
129+
}
130+
}

process/src/main.rs

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,14 @@
1-
use std::process::{Command, Stdio};
21
use process::*;
32

43
fn main() {
54
spawn_a_process();
65
process_io();
76
child();
87
kill();
8+
pipe();
99

1010
async_process_example();
1111
process_control_example();
1212
easy_process_example();
1313
}
1414

15-
fn spawn_a_process() {
16-
let output = Command::new("echo")
17-
.arg("Hello world")
18-
.output()
19-
.expect("Failed to execute command");
20-
21-
assert_eq!(b"Hello world\n", output.stdout.as_slice());
22-
}
23-
24-
fn process_io() {
25-
let echo_child = Command::new("echo")
26-
.arg("Oh no, a tpyo!")
27-
.stdout(Stdio::piped())
28-
.spawn()
29-
.expect("Failed to start echo process");
30-
31-
let echo_out = echo_child.stdout.expect("Failed to open echo stdout");
32-
33-
let sed_child = Command::new("sed")
34-
.arg("s/tpyo/typo/")
35-
.stdin(Stdio::from(echo_out))
36-
.stdout(Stdio::piped())
37-
.spawn()
38-
.expect("Failed to start sed process");
39-
40-
let output = sed_child.wait_with_output().expect("Failed to wait on sed");
41-
assert_eq!(b"Oh no, a typo!\n", output.stdout.as_slice());
42-
}
43-
44-
fn child() {
45-
let mut child = Command::new("/bin/cat")
46-
.arg("Cargo.toml")
47-
.spawn()
48-
.expect("failed to execute child");
49-
50-
let ecode = child.wait().expect("failed to wait on child");
51-
52-
assert!(ecode.success());
53-
}
54-
55-
fn kill() {
56-
let mut command = Command::new("yes");
57-
if let Ok(mut child) = command.spawn() {
58-
println!("Child's ID is {}", child.id());
59-
child.kill().expect("command wasn't running");
60-
} else {
61-
println!("yes command didn't start");
62-
}
63-
}

0 commit comments

Comments
 (0)