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
rustc find_second_smallest.rs
Once the program is compiled, you can run it using the following command:
Code snippet
./find_second_smallest
This will print the following output to the console:
Code snippet
The second smallest element is 5
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a Rust program to find the second smallest element in an array:
Rust
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
Once the program is compiled, you can run it using the following command:
Code snippet
This will print the following output to the console:
Code snippet