w3resource

Rust Array Concatenation Guide

Rust Arrays: Exercise-9 with Solution

Write a Rust program to create two arrays of integers, each with size 4, and initialize them with random values. Concatenate the two arrays and print the resulting array.

Sample Solution:

Rust Code:

use rand::Rng; // Import the rand crate to generate random numbers

fn main() {
    // Define an array with a size of 4 and initialize it with random values
    let mut arr1: [i32; 4] = [0; 4]; // Declare an array of type i32 (integer) with a size of 4 and initialize it with zeros
    let mut rng = rand::thread_rng(); // Initialize the random number generator
    
    for i in 0..4 { // Iterate over each index of the array
        arr1[i] = rng.gen_range(1..101); // Generate a random integer between 1 and 100 and assign it to the current index
    }

    // Define another array with a size of 4 and initialize it with random values
    let mut arr2: [i32; 4] = [0; 4]; // Declare another array of type i32 (integer) with a size of 4 and initialize it with zeros
    
    for i in 0..4 { // Iterate over each index of the array
        arr2[i] = rng.gen_range(1..101); // Generate a random integer between 1 and 100 and assign it to the current index
    }

    // Concatenate the two arrays
    let mut concatenated_array: [i32; 8] = [0; 8]; // Declare a new array with a size of 8 to hold the concatenated values
    
    for i in 0..4 { // Iterate over each index of the arrays
        concatenated_array[i] = arr1[i]; // Copy elements from the first array to the concatenated array
        concatenated_array[i + 4] = arr2[i]; // Copy elements from the second array to the concatenated array, starting from index 4
    }

    // Print the resulting concatenated array
    println!("Concatenated Array: {:?}", concatenated_array); // Print the resulting concatenated array using debug formatting
}

Output:

Concatenated Array: [33, 24, 93, 74, 77, 81, 33, 15]

Explanation:

Here is a brief explanation of the above Rust code:

  • use rand::Rng;: This line imports the rand::Rng trait from the "rand" crate, which provides functions for generating random numbers.
  • fn main() {: This line defines the main function, which is the entry point of the Rust program.
  • let mut arr1: [i32; 4] = [0; 4];: This line declares an array named 'arr1' of type 'i32' (integer) with a size of 4 and initializes it with zeros.
  • let mut rng = rand::thread_rng();: This line initializes the random number generator by creating a thread-local generator.
  • for i in 0..4 { ... }: This line starts a for loop that iterates over each index of the array arr1.
  • arr1[i] = rng.gen_range(1..101);: This line generates a random integer between 1 and 100 using the gen_range method of the random number generator 'rng', and assigns it to the current index 'i' of the array 'arr1'.
  • Similarly, let mut arr2: [i32; 4] = [0; 4]; and the subsequent loop initializes 'arr2' with random values.
  • let mut concatenated_array: [i32; 8] = [0; 8];: This line declares a new array named concatenated_array of type 'i32' (integer) with a size of 8 to hold the concatenated values.
  • The next loop copies elements from 'arr1' and 'arr2' into concatenated_array, ensuring that elements from 'arr2' are placed after the elements from 'arr1'.
  • Finally, println!("Concatenated Array: {:?}", concatenated_array); prints the resulting concatenated array to the console using debug formatting.

Rust Code Editor:

Previous: Rust Array Reversal Guide.
Next: Rust Array Sorting Guide.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/rust/collections_and_data_structures/rust-arrays-exercise-9.php