Q:

Rust program to create an array using the existing array

0

In this program, we will create an integer array using the existing array and then print both arrays.

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 create an array using the existing array is given below. The given program is compiled and executed successfully.

// Rust program to create an array 
// using existing array

fn main() {
   let mut arr1:[i32;5] = [1,2,3,4,5];
   
   let mut arr2=arr1;
   
   println!("Array1 : {:?}",arr1);
   println!("Array2 : {:?}",arr2);
   
   arr2[0] = 9;
   
   println!("\nArray1 : {:?}",arr1);
   println!("Array2 : {:?}",arr2);
}

Output:

Array1 : [1, 2, 3, 4, 5]
Array2 : [1, 2, 3, 4, 5]

Array1 : [1, 2, 3, 4, 5]
Array2 : [9, 2, 3, 4, 5]

Explanation:

Here, we created array arr2 using the existing array arr1. Then we modified the 1st element of arr2. After that, we printed the elements of both arrays.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now