ADVERTISEMENT
ADVERTISEMENT

Rust program to merge two arrays into third array

Rust | Array Example: Write a program to merge two arrays into a third array.
Submitted by Nidhi, on October 20, 2021

Problem Solution:

In this program, we will create three arrays of integers. Then we will merge two arrays into a third array.

Program/Source Code:

The source code to merge two arrays into a third array is given below. The given program is compiled and executed successfully.

// Rust program to merge two arrays 
// into 3rd array

fn main() {
    let mut arr1:[i32;5] = [0,1,2,3,4];
    let mut arr2:[i32;5] = [5,6,7,8,9];
    let mut arr3:[i32;10] = [0;10];
    
    let mut i:usize = 0;
    let mut j:usize = 0;
    
    while i<5
    {
        arr3[i]=arr1[i];
        i = i + 1;
    }
    
    while j<5
    {
        arr3[i]=arr2[j];
        i = i + 1;
        j = j + 1;
    }
    
    println!("arr1: {:?}",arr1);
    println!("arr2: {:?}",arr2);
    println!("arr3: {:?}",arr3);
}

Output:

arr1: [0, 1, 2, 3, 4]
arr2: [5, 6, 7, 8, 9]
arr3: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Explanation:

Here, we created two arrays of the integers with 5 elements and 3rd array with 10 elements initialized with 0. Then we copy the elements of arr1 and arr2 into arr3. After that, we printed the all 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.