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
This commit is contained in:
		
							parent
							
								
									342b233a0a
								
							
						
					
					
						commit
						27c1763a7a
					
				
					 2 changed files with 24 additions and 6 deletions
				
			
		|  | @ -3,16 +3,12 @@ use serde_json::{json, Value}; | ||||||
| 
 | 
 | ||||||
| mod display; | mod display; | ||||||
| mod json; | mod json; | ||||||
|  | mod stdin; | ||||||
| 
 | 
 | ||||||
| ////////////////////////////////////////////////////////////////////////////////
 | ////////////////////////////////////////////////////////////////////////////////
 | ||||||
| // Main
 | // Main
 | ||||||
| ////////////////////////////////////////////////////////////////////////////////
 | ////////////////////////////////////////////////////////////////////////////////
 | ||||||
| 
 | 
 | ||||||
| fn main() { | fn main() { | ||||||
|     let john: display::Person = display::Person { |     stdin::example(); | ||||||
|         fname: "John".to_string(), |  | ||||||
|         lname: "Cleese".to_string(), |  | ||||||
|         age: 82, |  | ||||||
|     }; |  | ||||||
|     println!("Person: {}", john) |  | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										22
									
								
								users/wpcarro/scratch/rust/src/stdin/mod.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								users/wpcarro/scratch/rust/src/stdin/mod.rs
									
										
									
									
									
										Normal file
									
								
							|  | @ -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)); | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue