about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2022-08-09T21·43-0700
committerclbot <clbot@tvl.fyi>2022-08-29T16·21+0000
commit27c1763a7aec455a117e3f47f7f8700f9d0c790e (patch)
tree00f14c4934df56b93eb2b88def1b5e807f52d897 /users
parent342b233a0a626aaad4ea32b1e5bf4f873afe7206 (diff)
feat(wpcarro/rust): Define stdin example r/4525
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
Diffstat (limited to 'users')
-rw-r--r--users/wpcarro/scratch/rust/src/main.rs8
-rw-r--r--users/wpcarro/scratch/rust/src/stdin/mod.rs22
2 files changed, 24 insertions, 6 deletions
diff --git a/users/wpcarro/scratch/rust/src/main.rs b/users/wpcarro/scratch/rust/src/main.rs
index da9e3d3c63..185e7236c5 100644
--- a/users/wpcarro/scratch/rust/src/main.rs
+++ b/users/wpcarro/scratch/rust/src/main.rs
@@ -3,16 +3,12 @@ use serde_json::{json, Value};
 
 mod display;
 mod json;
+mod stdin;
 
 ////////////////////////////////////////////////////////////////////////////////
 // Main
 ////////////////////////////////////////////////////////////////////////////////
 
 fn main() {
-    let john: display::Person = display::Person {
-        fname: "John".to_string(),
-        lname: "Cleese".to_string(),
-        age: 82,
-    };
-    println!("Person: {}", john)
+    stdin::example();
 }
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 0000000000..4be95afa45
--- /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));
+}