Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum Error {
- AddOverflow,
- ProdOverflow,
- }
- fn main() {
- let res = tester();
- match res {
- Ok(_) => println!("success"),
- Err(e) => {
- match e {
- Error::AddOverflow => panic!("add_overflow"),
- Error::ProdOverflow => panic!("prod_overflow")
- }
- }
- }
- }
- fn tester() -> Result<bool, Error> {
- let x: u32 = u32::MAX;
- let y: u32 = 1;
- let mut res: u32;
- res = add(y, y)?;
- println!("{res}");
- res = prod(x, x)?;
- println!("{res}");
- Ok(true)
- }
- fn add(x: u32, y: u32) -> Result<u32, Error> {
- let sum: u64 = (x as u64) + (y as u64);
- if sum > (u32::MAX as u64) {
- return Err(Error::AddOverflow);
- }
- Ok(sum as u32)
- }
- fn prod(x: u32, y: u32) -> Result<u32, Error> {
- let prod: u64 = (x as u64) * (y as u64);
- if prod > (u32::MAX as u64) {
- return Err(Error::ProdOverflow);
- }
- Ok(prod as u32)
- }
Advertisement
Add Comment
Please, Sign In to add comment