Rust program to create an array using the existing array

Rust | Array Example: Write a program to create an array using the existing array.
Last Updated : October 20, 2021

Problem Statement

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

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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