coding-horror/basic-computer-games · error
Failed to get input
Error message
Failed to get input
What it means
Same read_line().expect() pattern as the other Stars game inputs, but this call reads the initial yes/no answer to 'DO YOU WANT INSTRUCTIONS?'. The panic fires on a genuine io::Error from read_line. A secondary latent bug exists just below at line 126: need_instrut[..1] slices into the string without bounds checking, so if read_line returns Ok(0) (EOF, empty string) the slice itself panics with a separate index-out-of-bounds error before the parse logic runs.
Source
Thrown at 82_Stars/rust_JWB/src/main.rs:117
=======
use rand::Rng;
use std::io;
fn main() {
println!(
"{: >39}\n{: >57}\n\n\n",
"STARS", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"
);
// STARS - PEOPLE'S COMPUTER CENTER, MENLO PARK, CA
// A IS LIMIT ON NUMBER, M IS NUMBER OF GUESSES
let a: u32 = 101;
let m: u32 = 7;
let mut need_instrut = String::new();
println!("DO YOU WANT INSTRUCTIONS?");
io::stdin()
.read_line(&mut need_instrut)
.expect("Failed to get input");
if need_instrut[..1].to_ascii_lowercase().eq("y") {
println!("I AM THINKING OF A WHOLE NUMBER FROM 1 TO {}", a - 1);
println!("TRY TO GUESS MY NUMBER. AFTER YOU GUESS, I");
println!("WILL TYPE ONE OR MORE STARS (*). THE MORE");
println!("STARS I TYPE, THE CLOSER YOU ARE TO MY NUMBER.");
println!("ONE STAR (*) MEANS FAR AWAY, SEVEN STARS (*******)");
println!("MEANS REALLY CLOSE! YOU GET {} GUESSES.\n\n", m);
}
loop {
println!("\nOK, I AM THINKING OF A NUMBER, START GUESSING.\n");
let rand_number: i32 = rand::thread_rng().gen_range(1..a) as i32; // generates a random number between 1 and 100
// GUESSING BEGINS, HUMAN GETS M GUESSES
for i in 0..m {
let mut guess = String::new();
println!("YOUR GUESS?");View on GitHub (pinned to 5301155192)
Solutions
- Replace .expect() with a match that defaults to 'no instructions' on both Ok(0) and Err, since skipping instructions is a safe fallback.
- Replace the unguarded slice need_instrut[..1] with need_instrut.trim().to_ascii_lowercase().starts_with('y') to avoid the separate index-out-of-bounds panic on empty input.
- If you must index, use need_instrut.get(..1) which returns Option and handle None gracefully.
- For CI, prepend a 'n\n' or 'y\n' line at the start of piped input to satisfy this prompt before gameplay lines.
Example fix
// before
io::stdin()
.read_line(&mut need_instrut)
.expect("Failed to get input");
if need_instrut[..1].to_ascii_lowercase().eq("y") {
// after
let want = match io::stdin().read_line(&mut need_instrut) {
Ok(0) | Err(_) => false,
Ok(_) => need_instrut.trim().to_ascii_lowercase().starts_with('y'),
};
if want { Defensive patterns
Strategy: try-catch
Validate before calling
// Validate read_line result AND guard the slice access
let want_instr = match io::stdin().read_line(&mut need_instrut) {
Ok(0) | Err(_) => false,
Ok(_) => need_instrut.trim().to_ascii_lowercase().starts_with('y'),
};
if want_instr { /* print instructions */ } Try / catch
// Handle I/O error, EOF, AND empty-string slice safely
match io::stdin().read_line(&mut need_instrut) {
Ok(0) => { /* EOF: default to no instructions */ }
Err(e) => eprintln!("Input error: {e}"),
Ok(_) => {
// Safe alternative to need_instrut[..1] which panics on empty string
if need_instrut.trim().to_ascii_lowercase().starts_with('y') {
// print instructions
}
}
} Prevention
- Never use [..1] or [0] on a String without checking length — use .starts_with() or .chars().next() instead.
- Match read_line's Result and treat EOF (Ok(0)) as a distinct case from Err.
- Default to the safe branch (skip instructions) when input is unavailable.
- Test with empty input (echo -n '' | cargo run) to catch slice-bounds panics.
When it happens
Trigger: Piped input that closes before the first prompt is answered. Ctrl-D pressed at the 'DO YOU WANT INSTRUCTIONS?' prompt. A non-interactive environment (CI, Docker without -it) where stdin has no data at all.
Common situations: Running cargo run with input redirected from /dev/null or an empty file. Piping only gameplay guesses without first answering the instructions prompt. SSH session dropping before the first keystroke.
Related errors
AI-assisted analysis of coding-horror/basic-computer-games@5301155192 (2026-08-13).
Data as JSON: /api/errors/36e9b33e86344b9a.
Report an issue: GitHub.