RS.CLIPPY.NAIVE_BYTECOUNT

Use of naive `<slice>.filter(|&x| x == y).count()` to count byte values

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

What it does

Checks for naive byte counts

Why is this bad?

The bytecount crate has methods to count your bytes faster, especially for large slices.

Known problems

If you have predominantly small slices, the bytecount::count(..) method may actually be slower. However, if you can ensure that less than 2³²-1 matches arise, the naive_count_32(..) can be faster in those cases.

Example

let count = vec.iter().filter(|x| **x == 0u8).count();

Use instead:

let count = bytecount::count(&vec, 0u8);