nandi/rustnimpublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

031-saturating-checked.rs · 15 lines · 976 BRust Blame HistoryRaw
Add generics; transpile the part of cosmic-theme that is reachable ff34e1b nandithebull 3h ago1// Rust's explicit overflow policies. Plain `+` traps on signed and wraps on
2// unsigned in both languages; these are neither, so they are spelled out.
3fn main() {
4 println!("{} {}", 250u8.saturating_add(10), 5u8.saturating_sub(10));
5 println!("{} {}", 200u8.saturating_mul(2), 100u8.saturating_mul(2));
6 println!("{} {}", 127i8.saturating_add(1), (-128i8).saturating_sub(1));
7 println!("{} {}", 127i8.saturating_mul(2), (-128i8).saturating_mul(2));
8 println!("{} {}", i32::MAX.saturating_add(1), i32::MIN.saturating_sub(1));
9 println!("{:?} {:?}", 250u8.checked_add(5), 250u8.checked_add(10));
10 println!("{:?} {:?}", 5u8.checked_sub(5), 5u8.checked_sub(6));
11 println!("{:?} {:?}", 100u8.checked_mul(2), 200u8.checked_mul(2));
12 println!("{:?} {:?}", 127i8.checked_add(0), 127i8.checked_add(1));
13 println!("{:?} {:?}", (-128i8).checked_sub(0), (-128i8).checked_sub(1));
14 println!("{} {}", 16u16.saturating_sub(16), 16u16.saturating_sub(24));
15}