// Copyright 2026 Amber Hu
use std::io;
use std::io::prelude::*;
fn main() {
loop {
let num = get_number();
println!("{num} + 1 = {}", num+1);
}
// We can avoid crashing the program if we handle the error
//loop {
// let result = try_get_number();
// match result {
// Ok(n) => println!("{n} + 1 = {}", n+1),
// Err(msg) => println!("Could not read number: {msg}"),
// };
//}
}
#[allow(dead_code)]
pub fn get_number() -> u8 {
print!("Input a number: ");
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin()
.read_line(&mut input).unwrap();
let x : u8 = input.trim().parse().expect("Expected a number in [0,255]");
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Immediately crashes the process if it encounters an error. Not graceful!
x
}
#[allow(dead_code)]
fn try_get_number() -> Result<u8, std::num::ParseIntError> {
print!("Input a number: ");
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin()
.read_line(&mut input).unwrap();
let x : u8 = input.trim().parse()?;
// ^
// |
// Instead of crashing, propagate the error
Ok(x)
// Wrap our integer value in Ok(), to say we successfully parsed
}