25 releases

0.13.7 Dec 22, 2023
0.13.6 Nov 29, 2022
0.13.5 Feb 4, 2021
0.13.4 Apr 13, 2020
0.1.0 Feb 20, 2016

#14 in Development tools

Download history 46728/week @ 2023-12-23 65224/week @ 2023-12-30 73408/week @ 2024-01-06 76135/week @ 2024-01-13 81149/week @ 2024-01-20 82862/week @ 2024-01-27 145927/week @ 2024-02-03 131620/week @ 2024-02-10 129264/week @ 2024-02-17 126038/week @ 2024-02-24 115926/week @ 2024-03-02 90404/week @ 2024-03-09 89755/week @ 2024-03-16 78934/week @ 2024-03-23 82114/week @ 2024-03-30 69789/week @ 2024-04-06

337,301 downloads per month
Used in 257 crates (140 directly)

MIT license

105KB
1.5K SLoC

duct.rs Actions Status crates.io docs.rs

Duct is a library for running child processes. Duct makes it easy to build pipelines and redirect IO like a shell. At the same time, Duct helps you write correct, portable code: whitespace is never significant, errors from child processes get reported by default, and a variety of gotchas, bugs, and platform inconsistencies are handled for you the Right Way™.

Examples

Run a command without capturing any output. Here "hi" is printed directly to the terminal:

use duct::cmd;
cmd!("echo", "hi").run()?;

Capture the standard output of a command. Here "hi" is returned as a String:

let stdout = cmd!("echo", "hi").read()?;
assert_eq!(stdout, "hi");

Capture the standard output of a pipeline:

let stdout = cmd!("echo", "hi").pipe(cmd!("sed", "s/i/o/")).read()?;
assert_eq!(stdout, "ho");

Merge standard error into standard output and read both incrementally:

use duct::cmd;
use std::io::prelude::*;
use std::io::BufReader;

let big_cmd = cmd!("bash", "-c", "echo out && echo err 1>&2");
let reader = big_cmd.stderr_to_stdout().reader()?;
let mut lines = BufReader::new(reader).lines();
assert_eq!(lines.next().unwrap()?, "out");
assert_eq!(lines.next().unwrap()?, "err");

Children that exit with a non-zero status return an error by default:

let result = cmd!("false").run();
assert!(result.is_err());
let result = cmd!("false").unchecked().run();
assert!(result.is_ok());

Dependencies

~0.1–9.5MB
~50K SLoC