Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Operators and Expressions

The as cast operator

Reference for the Rust as operator: integer truncation and sign extension, float to int saturation, char and bool casts, pointer casts and safer alternatives.

expr as Type converts a value to another primitive type using fixed, always-defined rules. It is the explicit conversion between numeric types, from bool and char to integers, from u8 to char, from unit-only enums to integers, and between pointer types. A cast never fails and never panics; instead the rules silently truncate, wrap or saturate.

Because the result can lose information without any warning, many teams prefer From (lossless), TryFrom (checked) and methods like i32::try_from(x) for anything that is not obviously safe. as is still the right tool for deliberate bit-twiddling, array indexing with as usize, and float rounding.

Rust
let small = 300_i32 as u8;     // 44 (truncated)
let index = n as usize;
let code = 'A' as u32;         // 65

Try it

Rust Integer and float casts

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.

Rust char, bool, enums and safer alternatives

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

Rust
expr as Type
Source Target Behavior
integer integer Same width: reinterpret the bits. Narrower: keep the low bits. Wider: sign-extend if the source is signed, otherwise zero-extend.
integer float Nearest representable value (ties to even).
float integer Truncate toward zero; out-of-range values saturate to the target's MIN/MAX; NaN becomes 0.
f64 / f32 f32 / f64 Rounds to nearest when narrowing; widening is exact.
bool, char integer false/true as 0/1; char as its Unicode scalar value (then the integer rules).
u8 char The only integer that can be cast to char.
unit-only enum integer The discriminant.
reference or pointer raw pointer, usize Pointer casts; *const T to usize gives the address.
fn item / non-capturing closure fn pointer, usize Function pointer (usually coercion does this without as).

Returns: a value of the target type.

Errors: an invalid cast (for example u32 as char, String as u8, or an enum with data as an integer) is a compile-time error.

Usage notes

  • No panics, no warnings: as is the one numeric operation that ignores overflow checks. 300 as u8 is 44 even in debug builds. Constant out-of-range literals get a deny-by-default lint (overflowing_literals), but 300_i32 as u8 is fine.
  • Float to int is saturating since Rust 1.45. Before that it was undefined behavior for out-of-range values; do not rely on old advice.
  • Prefer conversions that say what they mean: u64::from(x) for widening, u8::try_from(x) for narrowing, x.clamp(..) before a cast for saturation, f64::round() before a cast for rounding (as truncates).
  • Precision loss: i64 as f64 loses bits above 2^53; i32 as f32 loses bits above 2^24.
  • Pointer and address casts: casting a pointer to usize is allowed; casting back and dereferencing needs unsafe and does not preserve provenance rules. Prefer ptr.addr() and the strict-provenance APIs.
  • Precedence: as binds tighter than *, + and comparisons, but looser than unary operators and method calls. a as usize < b is parsed as the start of generic arguments and fails; write (a as usize) < b. See Operator precedence.
  • Enum casts: only enums whose variants carry no data can be cast, and a #[repr(u8)]-style attribute fixes the discriminant width. Going from an integer back to an enum requires a match or TryFrom.
  • Trait-object and slice coercions use as too (&arr as &[i32]), but they are coercions that rarely need the keyword.