Created
February 5, 2024 22:08
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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