expression
Report a user-actionable parsing failure
A diagnostic should describe the failed operation and a useful correction. Here a port must be a nonzero u16. Use a distinct exit status for invalid input according to the program’s documented policy. Do not print raw secret-bearing input in error text. Fixture: Expected stdout (not executed): (empty) Expected stderr: 'invalid port: choose an integer from 1 to 65535\n' Expected exit: 2 Common mistake: Printing only failed or panicking on normal user input.
Formal Syntax & Structure
rust syntaxrust
match input.parse::<u16>() { Ok(n) => ..., Err(_) => ... }Working Example
Example Snippetrust
fn main() -> std::process::ExitCode { use std::process::ExitCode; let input = "70000"; match input.parse::<u16>() { Ok(n) if n != 0 => { println!("port={n}"); ExitCode::SUCCESS } _ => { eprintln!("invalid port: choose an integer from 1 to 65535"); ExitCode::from(2) } }}