#How to idiomatically potentially consume several lines from a buffer?

15 messages · Page 1 of 1 (latest)

late gust
#

I know why this is broken:

    while let Some(Ok(raw)) = lines.next() {
        let line = raw.trim();

        if line.starts_with("Running unittests") {
            test_suite_start(line, &mut output);
        } else if line.starts_with("running ") {
            num_tests_running(line, &mut output);
        } else if line.starts_with("test result: ok. 0 passed") {
            test_suite_result_no_tests(line, &mut output);
        } else if line.starts_with("test result: ok.") {
            test_suite_result_ok(line, &mut output);
        } else if line.ends_with("FAILED") {
            test_result_failed(line, &mut output);
            while let Some(true) = lines.peekable().peek().map(|item| &item.unwrap_or_default().starts_with('@')){
                let extra_output = lines.next();
                // we need to consume strings until one doesn't start with '@'
                do_something(extra_output)
            }
        } else if line.ends_with(" ... ok") {
            continue;
        }

I am trying to use lines after it moved. I am not sure the idiomatic way of doing this.

fleet cobalt
#

probably before even the while let Some(Ok(raw)) = lines.next() loop

late gust
#

let mut peekable_lines = lines.peekable();

#

? i don't think you mean another one also named lines?

fleet cobalt
#

I did

late gust
#

that is surprising

fleet cobalt
#

it's called shadowing

#

relieves you of having to think up another name

#

and it's often used for converting types

#

here you go from Lines to Peekable, you can treat it as a conversion

#

and you cannot use Lines anymore anyway

late gust
#
    let mut lines = BufReader::new(reader).lines();
    let mut lines = lines.peekable();
    while let Some(Ok(raw)) = lines.next() {
fleet cobalt
#

looks good

#

(except the unused mut warning)