title: Fastest idiomatic I/O routine in Rust for programming contests?
body: <p><em>My question has been partially answered, so I've revised it in response to things I've learned from comments and additional experiments.</em></p>
  
  <p>In summary, I want a fast I/O routine for programming contests, in which problems are solved with a single file and no external crates. It should read in a sequence of whitespace-separated tokens from a <code>BufRead</code> (either stdin or a file). The tokens may be integers, floats or ASCII words, separated by spaces and newlines, so it seems I should support <code>FromStr</code> types generically. A small minority of problems are interactive, meaning not all of the input is available initially, but it always comes in complete lines.</p>
  
  <p>For context, here's <a href="https://codeforces.com/blog/entry/67391?#comment-515711" rel="noreferrer">the discussion that led me to post here</a>. Someone wrote very fast custom code to parse integers directly from the <code>&amp;[u8]</code> output of <code>BufRead::fill_buf()</code>, but it's not generic in <code>FromStr</code>.</p>
  
  <p>Here is my <a href="https://codeforces.com/contest/1151/submission/55185471" rel="noreferrer">best solution so far</a> (emphasis on the <code>Scanner</code> struct):</p>
  
  <pre class="lang-rust prettyprint-override"><code>use std::io::{self, prelude::*};
  
  fn solve&lt;B: BufRead, W: Write&gt;(mut scan: Scanner&lt;B&gt;, mut w: W) {
      let n = scan.token();
      let mut a = Vec::with_capacity(n);
      let mut b = Vec::with_capacity(n);
      for _ in 0..n {
          a.push(scan.token::&lt;i64&gt;());
          b.push(scan.token::&lt;i64&gt;());
      }
      let mut order: Vec&lt;_&gt; = (0..n).collect();
      order.sort_by_key(|&amp;i| b[i] - a[i]);
      let ans: i64 = order
          .into_iter()
          .enumerate()
          .map(|(i, x)| a[x] * i as i64 + b[x] * (n - 1 - i) as i64)
          .sum();
      writeln!(w, "{}", ans);
  }
  
  fn main() {
      let stdin = io::stdin();
      let stdout = io::stdout();
      let reader = Scanner::new(stdin.lock());
      let writer = io::BufWriter::new(stdout.lock());
      solve(reader, writer);
  }
  
  pub struct Scanner&lt;B&gt; {
      reader: B,
      buf_str: String,
      buf_iter: std::str::SplitWhitespace&lt;'static&gt;,
  }
  impl&lt;B: BufRead&gt; Scanner&lt;B&gt; {
      pub fn new(reader: B) -&gt; Self {
          Self {
              reader,
              buf_str: String::new(),
              buf_iter: "".split_whitespace(),
          }
      }
      pub fn token&lt;T: std::str::FromStr&gt;(&amp;mut self) -&gt; T {
          loop {
              if let Some(token) = self.buf_iter.next() {
                  return token.parse().ok().expect("Failed parse");
              }
              self.buf_str.clear();
              self.reader
                  .read_line(&amp;mut self.buf_str)
                  .expect("Failed read");
              self.buf_iter = unsafe { std::mem::transmute(self.buf_str.split_whitespace()) };
          }
      }
  }
  </code></pre>
  
  <p>By avoiding unnecessary allocations, this <code>Scanner</code> is quite fast. If we didn't care about unsafety, it can be made even faster by, instead of doing <code>read_line()</code> into a <code>String</code>, doing <code>read_until(b'\n')</code> into a <code>Vec&lt;u8&gt;</code>, followed by <code>str::from_utf8_unchecked()</code>.</p>
  
  <p>However, I'd also like to know what's the fastest <strong>safe</strong> solution. Is there a clever way to tell Rust that what my <code>Scanner</code> implementation does is actually safe, eliminating the <code>mem::transmute</code>? Intuitively, it seems we should think of the <code>SplitWhitespace</code> object as <strong>owning</strong> the buffer until it's effectively dropped after it returns <code>None</code>.</p>
  
  <p>All else being equal, I'd like a "nice" idiomatic standard library solution, as I'm trying to present Rust to others who do programming contests.</p>