Rust Lang

Rust Std::Min and Std:: Max Functions

The std::cmp is a module in the Rust standard library that provides the types and traits to compare the values. The functionality of this module is to provide a set of generic comparison operations that can be used across all types that implement them, making it easy to compare the values of different types.

The primary purpose of std::cmp is to provide a set of comparison traits that define the behavior of comparison operations such as ==, !=, <, >, <=, and >=. Various types in the Rust ecosystem implement the defined traits and are very useful in determining the behavior of various operations that are applied to the instances of the data types.

In this tutorial, we will discuss about the std::cmp::min and the std::cmp::max functions to determine the minimum and the maximum of two values.

Rust Std::cmp::min

The min() function from the cmp module allows us to provide two arguments and returns the minimum value of two options.

The function signature is demonstrated as follows:

pub fn min<T: Ord>(x: T, y: T) -> T

 

The function takes two arguments of the same type T where T must implement the Ord trait. The role of the Ord trait is to define the total ordering on the value of the type T. This is required to determine which values are smaller or larger.

Note: The function returns the smallest two values based on the ordering that is defined in the Ord trait. If the two values are equal, the function returns either.

As you can guess, the main operation of the function is using the “<” operator in the Ord trait. The definition is as follows:

pub fn min<T: Ord>(x: T, y: T) -> T {
    if x < y { x } else { y }
}

 

You can learn more about that in the official documentation or the Rust source code.

fn main() {
        let a = 10;
        let b = 20;
        let c = std::cmp::min(a, b);
        println!("The minimum {}", c);
}

 

In the previous example, we start by defining two variables, a and b, which are initialized with the values of 10 and 20, respectively.

We then use the std::cmp::min function to find the minimum value between the variables and assign the result to a new variable which is “c”.

Finally, we print the values of a, b, and c using a formatted string.

We can also use the min function with a string or any type that implements the Ord trait. Consider the following example which demonstrates the min() function with a string type:

fn main() {
    let s1 = String::from("hello");
    let s2 = String::from("world");
    let s3 = std::cmp::min(s1, s2);
   
    println!("The minimum is {}", s3);
}

 

String comparison in Rust is based on the lexicographic ordering of the provided values.

Resulting Output:

The minimum is hello

 

Rust Std::cmp::max

As you can guess, the function is part of the Rust standard library and allows us to determine the maximum between two input values.

The function definition is as follows:

pub fn max<T: Ord>(x: T, y: T) -> T

 

Similarly, the T types must be the same type and implements the Ord trait. If you are using a custom type, you need to import and implement this trait on your type.

Example:

fn main() {
    let a = 10;
    let b = 20;
    let c = std::cmp::max(a, b);
    println!("The maximum is {}", c);
}

 

Resulting Output:

The maximum is 20

 

The same applies to strings and other types that implement the Ord trait.

Conclusion

We explored how to work with the std::cmp:min and std::cmp::max functions in Rust to determine the minimum and maximum between two values, respectively.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list