title: Reading from a TcpStream with Read::read_to_string hangs until the connection is closed by the remote end
body: <p>I'm attempting to implement the <a href="https://wiki.haskell.org/Roll_your_own_IRC_bot" rel="noreferrer">Haskell IRC bot tutorial</a> in Rust and am having some difficulty reading what the server sends me after connecting. What seems to happen is that I connect, read ~5 KB from the server, and then roughly 240 seconds later everything is dumped at once instead of being read line-by-line. The connection is closed by a ping timeout, which should happen eventually, since I don't yet have a ping-pong function to reply with. </p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>use std::io::{Read, Write};
  use std::net::TcpStream;
  
  fn main() {
      let mut stream = TcpStream::connect("irc.freenode.org:6667").unwrap();
  
      let _ = stream.write(b"NICK G-SERUFU\r\n");
      let _ = stream.write(b"USER G-SERUFU 0 * :brobot\r\n");
      let _ = stream.write(b"JOIN #tutbot-testing\r\n");
  
      let mut line = String::with_capacity(512);
      loop {
          let result = stream.read_to_string(&amp;mut line);
          match result {
              Ok(n) =&gt; println!("Received {} bytes", n),
              _ =&gt; {}
          }
          line.clear();
      }
  }
  </code></pre>
  
  <p>When I modify the loop a to use an array instead of a string, I immediately get the output I expect:</p>
  
  <pre><code>let mut line;
  loop {
      line = [0; 512];
      let result = stream.read(&amp;mut line);
      match result {
          Ok(n) =&gt; println!("Received {} bytes",n),
          _ =&gt; {},
      }
  }
  </code></pre>
  
  <p>My conclusion is that <code>stream.read_to_string(&amp;mut line)</code> is somehow the culprit. Why might that be the case? Is there something obvious that I'm overlooking?</p>
  
  <p>To be more specific, in the first case the output appears after the ping timeout, upon which the following is printed:</p>
  
  <pre class="lang-none prettyprint-override"><code>//Around 4 minutes elapse before anything is printed to console
  Received 5323 bytes
  Received 0 bytes
  Received 0 bytes
  Received 0 bytes
  //Continues to print "Received 0 bytes" since the connection has closed but I haven't broken out of the infinite loop
  </code></pre>
  
  <p>In the second case using the array, I receive the correct output almost immediately:</p>
  
  <pre class="lang-none prettyprint-override"><code>Received 64 bytes
  Received 51 bytes
  Received 512 bytes
  Received 512 bytes
  Received 350 bytes
  Received 512 bytes
  Received 512 bytes
  ...
  </code></pre>