title: Unable to pipe to or from spawned child process more than once
body: <p>I want to be able to use Rust to spawn a child shell, then repeatedly pass it arbitrary commands and process their outputs. I have found plenty of examples online showing me how to pass a single command and receive its single output, but I can't seem to be able to do it repeatedly.</p>
  
  <p>For instance, the following code hangs on the line after the comment. (I imagine maybe <code>read_to_string()</code> is blocking until it receives stdout from the child process, but if so I don't understand why that output isn't forthcoming..)</p>
  
  <pre><code>let mut child_shell = match Command::new("/bin/bash")
      .stdin(Stdio::piped())
      .stdout(Stdio::piped())
      .spawn()
  {
      Err(why) =&gt; panic!("couldn't spawn child_shell: {}", Error::description(&amp;why)),
      Ok(process) =&gt; process,
  };
  
  loop {
      {
          match child_shell.stdin.as_mut().unwrap().write("ls".as_bytes()) {
              Err(why) =&gt; panic!(
                  "couldn't send command to child shell: {}",
                  Error::description(&amp;why)
              ),
              Ok(_) =&gt; println!("sent command to child shell"),
          }
      }
  
      {
          let mut s = String::new();
          // ↓ hangs on this line ↓
          match child_shell.stdout.as_mut().unwrap().read_to_string(&amp;mut s) {
              Err(why) =&gt; panic!("couldn't read bash stdout: {}", Error::description(&amp;why)),
              Ok(_) =&gt; print!("bash responded with:\n{}", s),
          }
      }
  }
  </code></pre>
  
  <p>I'm a beginner in Rust and I think the problem is my limited understanding of the borrow-checker/referencing rules, since the above runs fine (for a single iteration) if I remove the loop instruction from the code and change the references to the innards of the <code>std::process::Child</code> struct to immutable; for instance from this:</p>
  
  <pre><code>child_shell.stdin.as_mut().unwrap().write("ls".as_bytes())
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code> child_shell.stdin.unwrap().write("ls".as_bytes())
  </code></pre>
  
  <p>Obviously, repeatedly running <code>ls</code> isn't my ultimate goal, and I know that I could just write a shell script and then have Rust repeatedly run it - but (apart from the goal of just learning more about Rust!) this is something I need to be able to do, at least in principle, for a more complicated project (which I'm happy to go into if it might prove relevant to any solutions, but it's probably way, way outside the scope of this question!)</p>
  
  <p>Finally, if it turns out that it isn't possible to use a child shell in this way, I'd nevertheless like to learn how to repeatedly/continuously pipe to and from a spawned process running some other arbitrary command, as I wasn't able to find any info in the Rust documentation, tutorials or on Stack Overflow.</p>