Rust program to compare two arrays using the equal to (==) operator

Rust | Array Example: Write a program to compare two arrays using the equal to (==) operator.
Submitted by Nidhi, on October 20, 2021

Problem Solution:

In this program, we will create three integer arrays with few elements and then we will compare arrays using equal to (==) operator.

Program/Source Code:

The source code to compare two arrays using the equal to (==) operator is given below. The given program is compiled and executed successfully.

// Rust program to compare two arrays 
// using the equal to (==) operator

fn main() {
   let mut arr1:[i32;5] = [1,2,3,4,5];
   let mut arr2:[i32;5] = [1,2,3,4,5];
   let mut arr3:[i32;5] = [5,4,3,2,1];
   
   if(arr1==arr2)
   {
       println!("arr1 and arr2 have similar elements");
   }
   else
   {
       println!("arr1 and arr2 does not have similar elements");
   }
   
   if(arr1==arr3)
   {
       println!("arr1 and arr3 have similar elements");
   }
   else
   {
       println!("arr1 and arr3 does not have similar elements");
   }
}

Output:

arr1 and arr2 have similar elements
arr1 and arr3 does not have similar elements

Explanation:

Here, we created three integer arrays, each of them containing 5 elements. Then we compared arrays using equal to (==) operator and printed the appropriate message.

Rust Arrays Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.