我正在尝试重现 Shepmasters 对这个问题的回答,但出现以下编译错误。
error[E0599]: the method `for_each` exists for struct `tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>`, but its trait bounds were not satisfied
--> src/main.rs:19:10
|
19 | .for_each(|s| async move { println!("> {:?}", s) })
| ^^^^^^^^ method cannot be called on `tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>` due to unsatisfied trait bounds
|
::: /home/.../tokio-1.7.1/src/io/util/lines.rs:10:1
|
10 | / pin_project! {
11 | | /// Read lines from an [`AsyncBufRead`].
12 | | ///
13 | | /// A `Lines` can be turned into a `Stream` with [`LinesStream`].
... |
29 | | }
30 | | }
| |_- doesn't satisfy `_: Iterator`
|
= note: the following trait bounds were not satisfied:
`tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>: Iterator`
which is required by `&mut tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>: Iterator`
Run Code Online (Sandbox Code Playgroud)
这是我的代码(Raspberry Pi 4 上的 Rust 1.52.1)
[dependencies]
futures = "0.3.15"
tokio = { version = "1", features = ["full"] }
Run Code Online (Sandbox Code Playgroud)
error[E0599]: the method `for_each` exists for struct `tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>`, but its trait bounds were not satisfied
--> src/main.rs:19:10
|
19 | .for_each(|s| async move { println!("> {:?}", s) })
| ^^^^^^^^ method cannot be called on `tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>` due to unsatisfied trait bounds
|
::: /home/.../tokio-1.7.1/src/io/util/lines.rs:10:1
|
10 | / pin_project! {
11 | | /// Read lines from an [`AsyncBufRead`].
12 | | ///
13 | | /// A `Lines` can be turned into a `Stream` with [`LinesStream`].
... |
29 | | }
30 | | }
| |_- doesn't satisfy `_: Iterator`
|
= note: the following trait bounds were not satisfied:
`tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>: Iterator`
which is required by `&mut tokio::io::Lines<tokio::io::BufReader<tokio::process::ChildStdout>>: Iterator`
Run Code Online (Sandbox Code Playgroud)
更一般地说,我如何才能更好地了解我需要导入哪些特征?在其他语言中,我只会查看给定类的方法,但在 Rust 中,我很难发现必要的特征。例如,我花了几天时间才找到期货地图futures::FutureExt。
我喜欢Tokio 文档中的这个示例。
下面是稍微修改后打印所有stderr行的代码:
use tokio::io::{BufReader, AsyncBufReadExt};
use tokio::process::Command;
use std::process::{Stdio};
async fn run_command(shell_cmd_str: &str) -> Result<()> {
let mut cmd = Command::new("sh");
cmd.args(&["-c", shell_cmd_str]);
cmd.stderr(Stdio::piped());
let mut child = cmd.spawn()
.expect("failed to spawn command");
let stderr = child.stderr.take()
.expect("child did not have a handle to stdout");
let mut stderr_reader = BufReader::new(stderr).lines();
tokio::spawn(async {
let status = child.await
.expect("child process encountered an error");
println!("child status was: {}", status);
});
while let Some(line) = stderr_reader.next_line().await? {
println!("Stderr line: {}", line);
}
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2024 次 |
| 最近记录: |