Rust Pass Input to Command Stdin

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
use std::{process::{Command, Stdio}, io::Write};

fn copy_to_clipboard_wcopy(s: String) {
	let mut wcopy = Command::new("wc")
		.arg("-c")
		.stdin(Stdio::piped())
		.stdout(Stdio::piped())
		.spawn()
		.expect("Failed to spawn `xclip`: please ensure you have it installed.");

	let mut stdin = wcopy.stdin.take().expect("Failed to write to stdin");
	std::thread::spawn(move || {
		stdin.write_all(s.as_bytes()).expect("Failed to write to stdin");
	});
	let output = wcopy.wait_with_output().expect("Failed to read stdout");
  println!("output: {}", String::from_utf8(output.stdout).unwrap());
}


fn main() {
    copy_to_clipboard_wcopy("El Psy Kongaroo.".to_string());
}

or

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
use std::{process::{Command, Stdio}, io::Write};

fn copy_to_clipboard_wcopy(s: String) {
	let mut wcopy = Command::new("wc")
		.arg("-c")
		.stdin(Stdio::piped())
		.stdout(Stdio::piped())
		.spawn()
		.expect("Failed to spawn `xclip`: please ensure you have it installed.");

	let mut stdin = wcopy.stdin.take().expect("Failed to write to stdin");
	stdin.write_all(s.as_bytes()).expect("Failed to write to stdin");
  drop(stdin);
	let output = wcopy.wait_with_output().expect("Failed to read stdout");
  println!("output: {}", String::from_utf8(output.stdout).unwrap());
}


fn main() {
    copy_to_clipboard_wcopy("El Psy Kongaroo.".to_string());
}