Rust program to create an array using the existing array

Rust | Array Example: Write a program to create an array using the existing array.
Submitted by Nidhi, on October 20, 2021

Problem Solution:

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

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.

Rust Arrays Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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