Skip to content

Instantly share code, notes, and snippets.

@mibmo
Created February 5, 2024 22:08
Show Gist options
  • Save mibmo/1def6ed76b019c999e64fba060f6a659 to your computer and use it in GitHub Desktop.
Save mibmo/1def6ed76b019c999e64fba060f6a659 to your computer and use it in GitHub Desktop.
Generic absolute difference helper function in Rust (for e.g. Durations or just anything implementing PartialOrd + Sub)
fn abs_diff<T: std::ops::Sub<Output = T> + PartialOrd>(a: T, b: T) -> T {
if a <= b {
b - a
} else {
a - b
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment