Q:

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

belongs to collection: Rust Arrays Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to create an array using the existing... >>
<< Rust program to create an array with constant size...