snix/users/wpcarro/scratch/rust/src/stdin/mod.rs
William Carroll 27c1763a7a feat(wpcarro/rust): Define stdin example
Pipe Rust strings to shell commands.

Change-Id: Id8afeed642d30c79e193fa9b353de081a5843eb5
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6197
Reviewed-by: wpcarro <wpcarro@gmail.com>
Autosubmit: wpcarro <wpcarro@gmail.com>
Tested-by: BuildkiteCI
2022-08-29 16:21:37 +00:00

22 lines
518 B
Rust

use std::io::Write;
use std::process::{Command, Stdio};
// Example of piping-in a string defined in Rust to a shell command.
pub fn example() {
let input = "Hello, world!";
let mut cat = Command::new("cat")
.stdin(Stdio::piped())
.spawn()
.ok()
.unwrap();
cat.stdin
.take()
.unwrap()
.write_all(&input.as_bytes())
.unwrap();
let output = cat.wait_with_output().unwrap();
println!("{}", String::from_utf8_lossy(&output.stdout));
}