I'm trying to reproduce Shepmasters answer to this question, but getting the following compilation error.
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`
This is my code (rust 1.52.1 on Raspberry Pi 4)
[dependencies]
futures = "0.3.15"
tokio = { version = "1", features = ["full"] }
use futures::StreamExt; // <-- claimed to be unused
use std::process::Stdio;
// use tokio::prelude::*; <-- doesn't exit
use tokio::{io::AsyncBufReadExt, io::BufReader, process::Command};
#[tokio::main]
async fn main() {
let mut child = Command::new("sudo")
.arg("ls")
.stdout(Stdio::piped())
.spawn()
.expect("Command failed");
let mut stdout = child.stdout.take().unwrap();
BufReader::new(stdout)
.lines()
.for_each(|s| async move { println!("> {:?}", s) })
.await;
}
More generally, how I can get better at learning which traits I need to import? In other languages I'd just look at the methods on a given class, but in rust I struggle to discover the necessary traits. E.g. to map Futures it took me days to find futures::FutureExt
.