Nicer arithmetics on primitive types in Rust

334 views Asked by At

Is there a nicer way of doing arithmetics on different primitive types (with auto-promotion up) than explicit casting and unwrapping?

For example in case like:

let a: u8 = 1;
let b: u16 = 2;
let c: u32 = 3;

can I somehow get rid of all the casts in:

let total: u64 = a.to_u64().unwrap() + b.to_u64().unwrap() + c.to_u64().unwrap();
1

There are 1 answers

1
Dogbert On BEST ANSWER

Yes, you can use as:

let total: u64 = a as u64 + b as u64 + c as u64;

More info on type casting: http://doc.rust-lang.org/reference.html#type-cast-expressions