coding-horror/basic-computer-games · error

Failed to read input

Error message

Failed to read input

What it means

The get_str_from_user helper for the Weekday calculator flushes stdout (so the prompt and input share a line) then calls read_line().expect("Failed to read input"). The panic fires on io::Error from read_line. On EOF (Ok(0)) the function returns an empty trimmed string silently — the caller then receives "" which may cause downstream parse failures or logic errors in the weekday calculation.

Source

Thrown at 95_Weekday/rust/src/main.rs:301

    WEEKDAY IS A COMPUTER DEMONSTRATION THAT
    GIVES FACTS ABOUT A DATE OF INTEREST TO YOU.
    ");
}

/**
 * gets a string from user input
 */
fn get_str_from_user(prompt:&str) -> String {
    //DATA
    let mut raw_input = String::new();

    //print prompt
    print!("{}",prompt);
    //flust std out //allows prompt to be on same line as input
    stdout().flush().expect("failed to flush");

    //get input and trim whitespaces
    io::stdin().read_line(&mut raw_input).expect("Failed to read input");

    //return raw input
    return raw_input.trim().to_string();
}

View on GitHub (pinned to 5301155192)

Solutions

  1. Change get_str_from_user to return Option<String> or Result<String, io::Error> instead of String; return None/Err on EOF or I/O error.
  2. In the caller, check for empty string or None and re-prompt or exit with a clear message.
  3. Replace .expect() with a match: Ok(0) => return None, Err(e) => return Err(e), Ok(_) => return Some(trimmed).
  4. For testing, pipe every date component the program will request.

Example fix

// before
fn get_str_from_user(prompt:&str) -> String {
    // ...
    io::stdin().read_line(&mut raw_input).expect("Failed to read input");
    return raw_input.trim().to_string();
}

// after
fn get_str_from_user(prompt:&str) -> Option<String> {
    // ...
    match io::stdin().read_line(&mut raw_input) {
        Ok(0) => return None,
        Ok(_) => return Some(raw_input.trim().to_string()),
        Err(e) => {
            eprintln!("Input error: {e}");
            return None;
        }
    }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Return Option<String> so callers can detect missing input
fn get_str_from_user(prompt: &str) -> Option<String> {
    let mut raw_input = String::new();
    print!("{}", prompt);
    stdout().flush().ok()?;
    match io::stdin().read_line(&mut raw_input) {
        Ok(0) => None,
        Ok(_) => Some(raw_input.trim().to_string()),
        Err(_) => None,
    }
}

Try / catch

// Caller side: handle None explicitly
let date_str = match get_str_from_user("Enter date: ") {
    Some(s) if !s.is_empty() => s,
    _ => {
        println!("No input received. Exiting.");
        return;
    }
};

Prevention

When it happens

Trigger: stdin pipe closed during date-component entry. A terminal session dropped while the program waits for input. Non-interactive execution where stdin provides no data.

Common situations: Scripting the Weekday calculator with piped date input that runs out of lines. Running in CI without interactive stdin. Testing with /dev/null as stdin.

Related errors


AI-assisted analysis of coding-horror/basic-computer-games@5301155192 (2026-08-13). Data as JSON: /api/errors/e1e20b12cdb6e55f. Report an issue: GitHub.