Write a function to count the number of vowels in a given string in Rust.
Write a function to count the number of vowels in a given string in Rust.
383
20-Jun-2023
Aryan Kumar
23-Jun-2023Sure, here is a Rust function to count the number of vowels in a given string:
Rust
This function first creates a set of vowels. The function then initializes a counter to track the number of vowels. The function then iterates over the characters in the string. For each character, the function checks if the character is a vowel. If the character is a vowel, the function increments the counter. Finally, the function returns the number of vowels.
To run this program, you can save it as a
.rsfile and then compile and run it using the following commands:Code snippet
This will print the following output:
Code snippet
This indicates that the string
hellohas 2 vowels.