Use the `as` keyword to convert primitive types in Rust, such as casting an i32 to a u8.
Use the as keyword to cast a value from one primitive type to another by placing the target type after the keyword. This operation performs a truncation or conversion based on the size of the target type.
fn main() {
let x: i32 = 42;
let y: u8 = x as u8;
println!("{y}");
}
Type casting with as is like pouring water from a large bucket into a small cup; you are forcing the data to fit into a different size. You use this when you need to convert numbers between different formats, such as turning a large integer into a smaller one for storage. Be careful, because if the number is too big for the new type, the extra data gets cut off.