expression
Allocate integer minor units without losing a remainder
Split a nonnegative amount in minor units across a known nonzero number of recipients. Give one extra unit to each of the first remainder recipients under an explicit deterministic policy. Expected stdout (not executed): [34, 33, 33] 100 Common mistake: Rounding each share independently and losing or creating units.
Formal Syntax & Structure
rust syntaxrust
total / parts; total % partsWorking Example
Example Snippetrust
fn main() { let total = 100u32; let parts = 3u32; let shares: Vec<_> = (0..parts).map(|idx| total / parts + u32::from(idx < total % parts)).collect(); println!("{:?} {}", shares, shares.iter().sum::<u32>());}