w3resource

Rust Array Search Guide

Rust Arrays: Exercise-5 with Solution

Write a Rust program to create an array of integers with size 7 and initialize it with random values. Search for a specific value in the array and print whether it exists or not.

Sample Solution:

Rust Code:

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

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

    // Define the value to search for
    let search_value = 50;

    // Search for the value in the array
    let exists = arr.iter().any(|&x| x == search_value); // Use the any() method on an iterator over the array elements to check if any element matches the search value

    // Print whether the value exists in the array or not
    if exists {
        println!("The value {} exists in the array.", search_value); // Print a message indicating that the value exists
    } else {
        println!("The value {} does not exist in the array.", search_value); // Print a message indicating that the value does not exist
    }
}

Output:

The value 50 does not exist in the array.

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 arr: [i32; 7] = [0; 7];: This line declares an array named 'arr' of type i32 (integer) with a size of 7 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..7 { ... }: This line starts a for loop that iterates over each index of the array arr.
  • arr[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 'arr'.
  • let search_value = 50;: This line defines the value to search for in the array.
  • let exists = arr.iter().any(|&x| x == search_value);: This line uses the "any()" method on an iterator over the array elements to check if any element matches the search value "search_value".
  • if exists { ... } else { ... }: This line starts a conditional statement to print whether the value exists in the array or not, based on the value of the 'exists' variable. If 'exists' is 'true', it prints a message indicating that the value exists; otherwise, it prints a message indicating that the value does not exist.

Rust Code Editor:

Previous: Rust Array Sum Guide.
Next: Rust Array Filtering 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-5.php