ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.