std-method

Unsigned midpoint

Find the integer average without overflowing the intermediate sum. Unsigned results truncate downward. Stabilized in 1.85. Expected stdout (not executed): 4294967294 3 Common mistake: Adding two large values first.

Formal Syntax & Structure

rust syntaxrust
a.midpoint(b) -> Self

Working Example

Example Snippetrust
fn main() {    println!("{}", (u32::MAX - 2).midpoint(u32::MAX));    println!("{}", 2u32.midpoint(5));}
Rust Reference