From 324cbad7a38f6b6206dff3c7c35e0d9398137a11 Mon Sep 17 00:00:00 2001 From: Paul Capron Date: Wed, 24 Oct 2018 15:21:05 +0200 Subject: [PATCH] =?UTF-8?q?factor:=20Don=E2=80=99t=20wrap=20stdin=20in=20B?= =?UTF-8?q?ufReader=20&=20use=20lock()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit stdin() is already buffered. Locking upfront explicitely avoid the overhead of mutex locking/unlocking everytime a new line is read. See https://stackoverflow.com/a/17546731 and https://www.reddit.com/r/rust/comments/3rj54u/how_can_i_read_char_by_char_from_stdin/cwpojn1/ The code cannot be simplified to “for line in stdin().lock().lines()” until non-lexical lifetimes are implemented. The compiler complains at the moment about a temporary value not living long enough/being dropped while still borrowed. See https://github.com/rust-lang/rust/issues/33520 This commit is related to issue #1103. --- src/factor/factor.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/factor/factor.rs b/src/factor/factor.rs index 97b92a4d3..2f5d5bb2f 100644 --- a/src/factor/factor.rs +++ b/src/factor/factor.rs @@ -23,7 +23,7 @@ use rand::distributions::{Distribution, Uniform}; use rand::{SeedableRng, thread_rng}; use rand::rngs::SmallRng; use std::cmp::{max, min}; -use std::io::{stdin, BufRead, BufReader}; +use std::io::{stdin, BufRead}; use std::num::Wrapping; use std::mem::swap; @@ -163,7 +163,8 @@ pub fn uumain(args: Vec) -> i32 { let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP).parse(args); if matches.free.is_empty() { - for line in BufReader::new(stdin()).lines() { + let stdin = stdin(); + for line in stdin.lock().lines() { for number in line.unwrap().split_whitespace() { print_factors_str(number); }