#215
Jun 21, 2026
215. next_multiple_of — Round Up to a Multiple Without the +m-1 Dance
Padding a length up to the next multiple of 8? The classic (n + 7) / 8 * 8 works right up until it overflows or you fat-finger the constant. next_multiple_of says exactly what you mean.
The hand-rolled version shows up everywhere alignment matters — buffer sizes, page rounding, table padding:
| |
Every integer type has next_multiple_of: the smallest value >= self that is a multiple of the argument. Already a multiple? It’s returned unchanged.
| |
When the rounded value would overflow, next_multiple_of panics — but checked_next_multiple_of hands you an Option instead, so you stay in control:
| |
One method, the intent on the page, and no off-by-one waiting to bite you.