RS.CLIPPY.MANUAL_SLICE_SIZE_CALCULATION

Manual slice size calculation

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

What it does

When a is &[T], detect a.len() * size_of::<T>() and suggest size_of_val(a) instead.

Why is this better?

  • Shorter to write
  • Removes the need for the human and the compiler to worry about overflow in the multiplication
  • Potentially faster at runtime as rust emits special no-wrapping flags when it calculates the byte length
  • Less turbofishing

Example

let newlen = data.len() * size_of::<i32>();

Use instead:

let newlen = size_of_val(data);

Configuration

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml

    (default: current version)