RS.CLIPPY.CAST_LOSSLESS

Casts using `as` that are known to be lossless, e.g., `x as u64` where `x: u8`

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: cast_lossless. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for casts between numeric types that can be replaced by safe conversion functions.

Why is this bad?

Rust's as keyword will perform many kinds of conversions, including silently lossy conversions. Conversion functions such as i32::from will only perform lossless conversions. Using the conversion functions prevents conversions from becoming silently lossy if the input types ever change, and makes it clear for people reading the code that the conversion is lossless.

Example

fn as_u64(x: u8) -> u64 {
    x as u64
}

Using ::from would look like this:

fn as_u64(x: u8) -> u64 {
    u64::from(x)
}