nandi/rustnimpublic Fork 0
7db79919131ab55e23a1730bf78c360e05d9977e
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.

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