Created
July 1, 2016 15:51
-
-
Save jess-sol/4b7ec3d0b8cbfadbfbcf6e905102ef4f to your computer and use it in GitHub Desktop.
Communicate with Subprocess using Rust. Uses subprocess_communicate and encoding crates. Demonstrates encoding/decoding UTF8 strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate subprocess_communicate; | |
| extern crate encoding; | |
| use std::path::Path; | |
| use std::process::{Command, Stdio, Child}; | |
| use self::subprocess_communicate::subprocess_communicate; | |
| use self::encoding::all::UTF_8; | |
| use self::encoding::{Encoding, EncoderTrap, DecoderTrap}; | |
| pub fn print_text() { | |
| let mut process = Command::new("/bin/cat") | |
| .stdin(Stdio::piped()) | |
| .stdout(Stdio::piped()) | |
| .stderr(Stdio::piped()) | |
| .spawn().unwrap(); | |
| let encoded_stdin = UTF_8.encode("testdata", EncoderTrap::Strict).unwrap(); | |
| let (ret_stdout, ret_stderr, err) = subprocess_communicate( | |
| &mut process, | |
| &encoded_stdin, | |
| Some(8), | |
| None, | |
| true | |
| ); | |
| let decoded_stdout = UTF_8.decode(&ret_stdout, DecoderTrap::Strict); | |
| assert_eq!(decoded_stdout.unwrap(), "testdata"); | |
| assert_eq!(String::from_utf8(ret_stdout).unwrap(), "testdata"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment