title: What corner case am I missing in my Rust emulation of C++'s `std::cin >>`?
body: <p>My plan is to write a simple method which does exactly what <code>std::cin &gt;&gt;</code> from the C++ standard library does:</p>
  
  <pre><code>use std::io::BufRead;
  
  pub fn input&lt;T: std::str::FromStr&gt;(handle: &amp;std::io::Stdin) -&gt; Result&lt;T, T::Err&gt; {
      let mut x = String::new();
      let mut guard = handle.lock();
      loop {
          let mut trimmed = false;
          let available = guard.fill_buf().unwrap();
          let l = match available.iter().position(|&amp;b| !(b as char).is_whitespace()) {
              Some(i) =&gt; {
                  trimmed = true;
                  i
              }
              None =&gt; available.len(),
          };
          guard.consume(l);
          if trimmed {
              break;
          }
      }
      let available = guard.fill_buf().unwrap();
      let l = match available.iter().position(|&amp;b| (b as char).is_whitespace()) {
          Some(i) =&gt; i,
          None =&gt; available.len(),
      };
      x.push_str(std::str::from_utf8(&amp;available[..l]).unwrap());
      guard.consume(l);
      T::from_str(&amp;x)
  }
  </code></pre>
  
  <p>The <code>loop</code> is meant to trim away all the whitespace before valid input begins. The <code>match</code> block outside the loop is where the length of the valid input (that is, before trailing whitespaces begin or <code>EOF</code> is reached) is calculated.</p>
  
  <p>Here is an example using the above method.</p>
  
  <pre><code>let handle = std::io::stdin();
  let x: i32 = input(&amp;handle).unwrap();
  println!("x: {}", x);
  let y: String = input(&amp;handle).unwrap();
  println!("y: {}", y);
  </code></pre>
  
  <p>When I tried a few simple tests, the method works as intended. However, when I use this in online programming judges like the one in codeforces, I get a complaint telling that the program sometimes stays idle or that the wrong input has been taken, among other issues, which leads to suspecting that I missed a corner case or something like that. This usually happens when the input is a few hundreds of lines long.</p>
  
  <p>What input is going to break the method? What is the correction?</p>