How to write a method which works with any type int?
The Rust Programming Language Forum
How to write a method which works with any type int?
Recently I needed a method converting time in 24 hours format in the twelve hours format. AI gave me a perfect solution as: pub fn convert_24_to_12(hour_24: u32) -> Result<(u32, bool), Box<dyn Error>> { // Validate ranges if hour_24 > 23 { return Err("Hour must be 0-23".into()); } // Convert to 12-hour format let hour_12 = match hour_24 % 12 { 0 => 12, h => h, }; Ok((hour_12, hour_24 > 11)) } However when I call the method with hours as u8,...
0 comments
No comments yet.