rust-lang / rust-lang/rust-clippy

BufReader Stdin or StdinLock

Open
#6,755 12 comments 2 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

A-lint good first issue L-perf T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Suggest using StdinLock rather than BufReader::new(io::stdin()) since it is already buffered. We might also want to take care of let stdin = io::stdin(); BufReader::new(stdin) and also BufReader::new(stdinlock) (there could be people doing this).

Categories (optional)
  • Kind: perf

Locking stdin also provide benefit for not having to re-lock it on every read.

std::io::stdin() already is buffered. Re-buffering it only increase the memcpy calls.

Source https://users.rust-lang.org/t/correct-way-for-taking-large-input-from-keyboard/55304/11
Related to https://github.com/rust-lang/rust-clippy/issues/1805

For example:

  • Recommend locking of stdin to reduce re-locking
  • Suggest not re-buffering stdin lock which already implements BufRead
Drawbacks

None.

Example
let reader = BufReader::new(io::stdin());  // this could a separate variable or StdinLock

Could be written as:

let stdin = io::stdin();
let reader = stdin.lock();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.