---
title: "Write a Rust program to find the second smallest element in an array."  
description: "Write a Rust program to find the second smallest element in an array."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158838/write-a-rust-program-to-find-the-second-smallest-element-in-an-array  
category: "rust"  
tags: ["programs", "rust"]  
reading_time: 2 minutes  

---

# Write a Rust program to find the second smallest element in an array.

Write a [Rust program](https://www.mindstick.com/forum/158800/create-a-rust-program-to-find-the-gcd-greatest-common-divisor-of-two-numbers) to find the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) [smallest](https://yourviews.mindstick.com/view/81880/royal-families-of-world-s-smallest-islands-do-fishing) [element in an array](https://www.mindstick.com/forum/156668/check-an-element-in-an-array-via-jquery).

## Replies

### Reply by Aryan Kumar

Sure, here is a [Rust](https://www.mindstick.com/forum/160170/explain-rust-s-ownership-borrowing-and-lifetimes-system) [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) to find the second [smallest element](https://www.mindstick.com/forum/157951/write-a-program-to-find-the-second-smallest-element-in-a-given-array-of-integers) in an array:

Rust

```plaintext
fn find_second_smallest(array: &[i32]) -> i32 {
    let mut smallest = array[0];
    let mut second_smallest = array[1];

    for element in array {
        if element < smallest {
            second_smallest = smallest;
            smallest = element;
        } else if element < second_smallest {
            second_smallest = element;
        }
    }

    return second_smallest;
}

fn main() {
    let array = [10, 5, 2, 7, 3, 1, 9, 8, 6];
    let second_smallest = find_second_smallest(&array);
    println!("The second smallest element is {}", second_smallest);
}
```

This program first defines a function called find_second_smallest that takes an array of i32 numbers as input and returns the second smallest element in the array. The function first initializes two variables, smallest and second_smallest, to the first two elements in the array. Then, the function iterates through the array, comparing each element to smallest and second_smallest. If an element is smaller than smallest, then second_smallest is assigned the value of smallest and smallest is assigned the value of the element. If an element is smaller than second_smallest but not smaller than smallest, then second_smallest is assigned the value of the element.

The main function of the program then calls the find_second_smallest function with the array [10, 5, 2, 7, 3, 1, 9, 8, 6] as input. The result of the function call is then printed to the console.

To run the program, you can save it as a file called find_second_smallest.rs and then compile it using the following command:

Code snippet

```plaintext
rustc find_second_smallest.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./find_second_smallest
```

This will print the following output to the console:

Code snippet

```plaintext
The second smallest element is 5
```


---

Original Source: https://www.mindstick.com/forum/158838/write-a-rust-program-to-find-the-second-smallest-element-in-an-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
