9

I'm trying to iterate over logs from a docker container by using the bollard crate.

Here's my code:

use std::default::Default;

use bollard::container::LogsOptions;
use bollard::Docker;

fn main() {
    let docker = Docker::connect_with_http_defaults().unwrap();

    let options = Some(LogsOptions::<String>{
        stdout: true,
        ..Default::default()
    });

    let data = docker.logs("2f6c52410d", options);
    // ...
}

docker.logs() returns impl Stream<Item = Result<LogOutput, Error>>. I'd like to iterate over the results, but I have no idea how to do that. I've managed to find an example that uses try_collect::<Vec<LogOutput>>() from the future_utils crate, but I'd like to iterate over the results in a while loop instead of collecting the results in a vector. I know that I can iterate over a vector, but performing tasks in a loop will be better for my use case.

I've tried to call poll_next() method for the stream, but it requires a mysterious Context object which I don't understand. The poll_next() method was unavailable until I've used pin_mut!() macro on the stream.

How do I iterate over stream? What should I read to understand what's going on here? I know that the streams are related to Futures, but calling await or next() doesn't work here.

1 Answer 1

13

You typically bring in your library of choice's StreamExt trait, and then do something like

while let Some(foo) = stream.next().await {
    // ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you elaborate? What do you mean by bring in your library of choice's StreamExt trait? The stream doesn't have the next() method.
@Djent futures::stream::StreamExt adds extension methods like next() for Streams.
Tokio also has one. Not sure what the difference is between them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.