RS.CLIPPY.READ_LINE_WITHOUT_TRIM

Calling `Stdin::read_line`, then trying to parse it without first trimming

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: read_line_without_trim. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Looks for calls to [Stdin::read_line] to read a line from the standard input into a string, then later attempting to use that string for an operation that will never work for strings with a trailing newline character in it (e.g. parsing into a i32).

Why is this bad?

The operation will always fail at runtime no matter what the user enters, thus making it a useless operation.

Example

let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read a line");
let num: i32 = input.parse().expect("Not a number!");
assert_eq!(num, 42); // we never even get here!

Use instead:

let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read a line");
let num: i32 = input.trim_end().parse().expect("Not a number!");
//                  ^^^^^^^^^^^ remove the trailing newline
assert_eq!(num, 42);