From 27c1763a7aec455a117e3f47f7f8700f9d0c790e Mon Sep 17 00:00:00 2001 From: William Carroll Date: Tue, 9 Aug 2022 14:43:25 -0700 Subject: 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 Autosubmit: wpcarro Tested-by: BuildkiteCI --- users/wpcarro/scratch/rust/src/stdin/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 users/wpcarro/scratch/rust/src/stdin/mod.rs (limited to 'users/wpcarro/scratch/rust/src/stdin/mod.rs') diff --git a/users/wpcarro/scratch/rust/src/stdin/mod.rs b/users/wpcarro/scratch/rust/src/stdin/mod.rs new file mode 100644 index 000000000000..4be95afa4547 --- /dev/null +++ b/users/wpcarro/scratch/rust/src/stdin/mod.rs @@ -0,0 +1,22 @@ +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)); +} -- cgit 1.4.1