keyword

Rust as: cast a number or rename an import

Use `as` for permitted explicit casts or import aliases. A numeric cast can lose information; choose a checked conversion when rejection matters. Expected output (not executed): 1.25 Why: Casting before division makes the calculation floating point. Common mistake: A narrowing integer cast can truncate bits; it is not a range check.

Formal Syntax & Structure

rust syntaxrust
value as Type; use path as Alias;

Working Example

Example Snippetrust
fn main() {    let grams = 1250_u32;    let kilograms = grams as f64 / 1000.0;    println!("{kilograms:.2}");}
Rust Reference