Rust Reference ยท Keywords and Statements
The as keyword
Reference for Rust as: primitive casts between numbers, char, bool and pointers, truncation and saturation rules, and renaming imports with use ... as.
as has two unrelated jobs. In an expression, value as Type performs a primitive cast: a fixed set of conversions between numeric types, bool/char to integers, u8 to char, and pointer conversions. In a use or extern crate declaration, path as name gives an item a different local name.
Casts with as never fail and never panic, which is also their danger: when the value does not fit, the result is silently truncated or saturated. For conversions that should be checked, prefer From, TryFrom or try_into().
let big: i32 = 300; let small = big as u8; // 44 (truncated) use std::collections::HashMap as Map;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
expr as Type use path::Item as NewName; use path::Trait as _; // import a trait without naming it extern crate foo as bar;
| Cast | Behavior |
|---|---|
| integer to integer | Same width: reinterpret bits. Narrowing: keep low bits. Widening: sign-extend if the source is signed, zero-extend otherwise. |
| float to integer | Rounds toward zero, saturates at the target's min/max, NaN becomes 0. |
| integer to float | Nearest representable value (may lose precision). |
f64 to f32 |
Rounds to nearest; out-of-range becomes infinity. |
bool / char to integer |
false = 0, true = 1; a char gives its code point (truncated if the target is too narrow). |
u8 to char |
The only integer type that casts to char. Use char::from_u32 for others. |
| enum to integer | Only for enums with no fields; gives the discriminant. |
| pointer casts | *const T to *mut T, to other pointer types, and to/from usize. |
Usage notes
- No integer cast can panic, even in debug builds with overflow checks on. Wraparound by
asis defined behavior. - Float to int is saturating since Rust 1.45. Earlier versions had undefined behavior for out-of-range values.
- Prefer
From/Intofor lossless conversions (i64::from(x)) andTryFromwhere values might not fit. These document intent and catch mistakes. - Casting
&Tto a pointer isr as *const T; going back requiresunsafeto dereference. - Precedence:
asbinds tighter than*and+but looser than unary operators and method calls:a as f64 / b as f64works, whilea as usize < bcan confuse the parser (it reads<as generics), so wrap it in parentheses. - Renaming:
use ... as _imports a trait's methods without a name, handy to avoid collisions. - Casting a function item to a pointer type works:
main as fn().