Sure, here is a Rust function to find the maximum value in a vector of numbers:
Rust
fn find_mavalue(vector: &Vec<i32>) -> i32 {
let mut mavalue = vector[0];
for number in vector {
if number > mavalue {
mavalue = number;
}
}
return mavalue;
}
fn main() {
let vector = vec![10, 4, 2, 9, 7, 5, 3, 8, 6];
let mavalue = find_mavalue(&vector);
println!("The maximum value in the vector is {}", mavalue);
}
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
rustc find_mavalue.rs
Once the program is compiled, you can run it using the following command:
Code snippet
./find_mavalue
This will print the following output to the console:
Code snippet
The maximum value in the vector is 10
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 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