Skip to main content

lines

Function lines 

Source
pub fn lines() -> Lines
Available on crate feature process only.
Expand description

Create reader to access output line-by-line.

The items will be available for read as soon as they are piped from the child, without having to wait for the child to finish first.

Note that for small outputs, it’s usually better to simply use string() and call .lines() on the result.

§Example

use cu::pre::*;

let (child, mut lines) = cu::which("bash")?.command()
    .args(["-c", r#"for i in {1..5}; do echo "Line $i"; done"# ])
    .stdout(cu::pio::lines())
    .stdie_null()
    .spawn()?;

assert_eq!(lines.next().unwrap()?, "Line 1");
assert_eq!(lines.next().unwrap()?, "Line 2");
assert_eq!(lines.next().unwrap()?, "Line 3");
assert_eq!(lines.next().unwrap()?, "Line 4");
assert_eq!(lines.next().unwrap()?, "Line 5");
assert!(lines.next().is_none());

child.wait_nz()?;

§Blocking

Since the iterator blocks the thread while waiting for the next line to be available, use co_lines when in an async context.