Skip to content
Justice Tech School

Casting with as

Use as to convert between numeric types. Narrowing may truncate.

Example
fn main() {
    let a = 3.99_f64;
    let b = a as i32;
    let c = 300_i32 as u8;
    let d = 'A' as u32;
    println!("{} {} {}", b, c, d);
}

Result

3 44 65

Exercise

Convert.

x  f64

Quiz Test

What is 300 as u8?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.