input_macro

Function read_and_parse_with_eof

Source
pub fn read_and_parse_with_eof<T: FromStr>() -> Result<Option<T>, InputError<T::Err>>
where T::Err: Debug + Display,
Expand description

Reads one line from stdin and attempts to parse it into T. Returns Ok(None) if EOF is reached.

This function is similar to read_and_parse, but handles EOF gracefully by returning None instead of an error. Useful for input loops where EOF indicates no more input.

§Errors

Returns InputError::Io on read errors.
Returns InputError::Parse if parsing fails.

§Examples

// If the user presses Ctrl-D (on Unix) or Ctrl-Z (on Windows) to signal EOF:
let value: Option<i32> = read_and_parse_with_eof().unwrap();
match value {
    Some(v) => println!("Got: {}", v),
    None => println!("No more input"),
}