How to find the Minimum of Two Numbers in Rust


How to find the Minimum of Two Numbers in Rust ?

Answer

To find the minimum of two numbers in Rust, you can use the std::cmp::min function.



✐ Examples

1 Minimum of Two Numbers

In this example,

  1. We declare two variables a and b and assign values to them.
  2. We use the std::cmp::min function to find the minimum of a and b.
  3. We print the minimum value.

Rust Program

// Minimum of Two Numbers
fn main() {
	let a = 10;
	let b = 15;
	let min_val = std::cmp::min(a, b);
	println!("Minimum of {} and {} is: {}", a, b, min_val);
}

Output

Minimum of 10 and 15 is: 10

Summary

In this tutorial, we learned How to find the Minimum of Two 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 ?