Why is this an issue?

The std::cmp::min and std::cmp::max functions in Rust are useful for clamping values within a specified range. However, if these functions are mistakenly swapped, the result will not behave as intended. Instead of clamping the value within the desired range, the outcome will be a constant value, which is likely not the intended behavior.

How to fix it

To fix this issue, ensure that min and max are used correctly to clamp the value between the desired range. The correct usage should ensure that the value is clamped between the minimum and maximum bounds.

Code examples

Noncompliant code example

min(0, max(100, x))

// or

x.max(100).min(0)

Compliant solution

max(0, min(100, x))

// or

x.min(100).max(0)

Resources

Documentation