How to find the Maximum of Three Numbers in Rust - Step by Step Examples



How to find the Maximum of Three Numbers in Rust ?

Answer

To find the maximum of three numbers in Rust, you can use the `std::cmp::max` function.



✐ Examples

1 Find Maximum of Three Numbers

In this example,

  1. We import the std::cmp module to use the `max` function.
  2. We declare three variables a, b, and c with the numbers to compare.
  3. We use the `max` function to compare the numbers and get the maximum.

Rust Program

fn main() {
    let a = 5;
    let b = 8;
    let c = 3;
    let max = std::cmp::max(std::cmp::max(a, b), c);
    println!("Maximum of three numbers is: {}", max);
}

Output

Maximum of three numbers is: 8

Summary

In this tutorial, we learned How to find the Maximum of Three Numbers in Rust language with well detailed examples.




More Rust Comparison Operations Tutorials

  1. How to find the Maximum of Two Numbers in Rust ?
  2. How to find the Minimum of Two Numbers in Rust ?
  3. How to find the Maximum of Three Numbers in Rust ?
  4. How to find the Minimum of Three Numbers in Rust ?