Advertisement
Guest User

Untitled

a guest
Apr 29th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. use std::collections::HashMap;
  2. use std::io::{self};
  3. use std::process::Command;
  4. use std::process::Child;
  5.  
  6. fn execute(children: &mut HashMap<u32, Child>, arg: &str) {
  7. let child = Command::new("/usr/bin/echo")
  8. .arg(arg)
  9. .spawn();
  10.  
  11. match child {
  12. Ok(child) => {
  13. // QUESTION: this appears to work, but should be &child to avoid copying?
  14. // doing merely that fails claiming the lifetime expires, so how to do it correctly?
  15. children.insert(child.id(), child);
  16. }
  17. Err(err) => {
  18. eprintln!("failed to spawn: {}", err);
  19. }
  20. }
  21. }
  22.  
  23. fn main() {
  24. let mut children: HashMap<u32, Child> = HashMap::new();
  25.  
  26. let stdin = io::stdin();
  27. for line in stdin.lines() {
  28. let input = String::from(&line.unwrap());
  29. for word in input.split_whitespace() {
  30. execute(&mut children, word);
  31. }
  32. }
  33.  
  34. dbg!(&children);
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement