Implement a function to find the maximum value in a vector of numbers in Rust.
Implement a function to find the maximum value in a vector of numbers in Rust.
357
20-Jun-2023
Aryan Kumar
23-Jun-2023Sure, here is a Rust function to find the maximum value in a vector of numbers:
Rust
This function first defines a function called find_mavalue that takes a reference to a Vec of i32 as input and returns an i32. The function first initializes a variable called mavalue to the first element of the vector. Then, the function iterates through the vector, comparing each element to the mavalue variable. If the element is greater than the mavalue variable, then the mavalue variable is updated with the element. Finally, the function returns the mavalue variable.
The main function of the program then creates a vector called vector with the values 10, 4, 2, 9, 7, 5, 3, 8, 6. The function then calls the find_mavalue function with the vector vector 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_mavalue.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