RBE types cast

This commit is contained in:
Dan Buch 2021-10-25 08:54:04 -04:00
parent 6d6e600720
commit d326a055f7
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7
2 changed files with 54 additions and 0 deletions

View File

@ -2,8 +2,10 @@
/hello/*
/primitives/*
/variable_bindings/*
/types/*
!/custom_types/*.rs
!/hello/*.rs
!/primitives/*.rs
!/variable_bindings/*.rs
!/types/*.rs

View File

@ -0,0 +1,52 @@
#![allow(overflowing_literals)]
fn main() {
let decimal = 65.4321_f32;
// let integer: u8 = decimal;
let integer = decimal as u8;
let character = integer as char;
// let character = decimal as char;
println!("Casting: {} -> {} -> {}", decimal, integer, character);
println!("1000 as u16 is: {}", 1000 as u16);
println!("1000 as u8 is: {}", 1000 as u8);
println!(" -1 as u8 is: {}", (-1i8) as u8);
println!("1000 mod 256 is: {}", 1000 % 256);
println!(" 128 as a i16 is: {}", 128 as i16);
println!(" 128 as a i8 is: {}", 128 as i8);
println!("1000 as a u8 is: {}", 1000 as u8);
println!(" 232 as a i8 is: {}", 232 as i8);
println!("300.0 as u8 is {}", 300.0_f32 as u8);
println!("-100.0 as u8 is {}", -100.0_f32 as u8);
println!("nan as u8 is {}", f32::NAN as u8);
unsafe {
println!(
"300.0 as u8 is {} (unchecked)",
300.0_f32.to_int_unchecked::<u8>()
);
println!(
"-100.0 as u8 is {} (unchecked)",
(-100.0_f32).to_int_unchecked::<u8>()
);
println!(
"nan as u8 is {} (unchecked)",
f32::NAN.to_int_unchecked::<u8>()
);
}
}