Q:

Rust program to add two integer arrays

belongs to collection: Rust Arrays Programs

0

In this program, we will create two arrays of integer elements then we will add both arrays and print the result.

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 add two integer arrays is given below. The given program is compiled and executed successfully.

// Rust program to add two arrays

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

Output:

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

Explanation:

Here, we created two arrays of integers with 5 elements and then we added both arrays and printed the addition of array elements.

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

total answers (1)

Rust Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to swap adjacent elements of the arra... >>
<< Rust program to sort an array in descending order ...