Sure, here is a Rust function to check if a given string is a pangram:
Rust
fn is_pangram(string: &str) -> bool {
let alphabet: &str = "abcdefghijklmnopqrstuvwxyz";
let mut seen_letters: [bool; 26] = [false; 26];
for c in string.chars() {
let index = c.to_ascii_lowercase() as usize - 'a' as usize;
seen_letters[index] = true;
}
for i in 0..26 {
if !seen_letters[i] {
return false;
}
}
return true;
}
fn main() {
let string = "The quick brown fox jumps over the lazy dog";
let is_pangram = is_pangram(string);
println!("The string {} is {}a pangram", string, if is_pangram { "" } else { "not "));
}
This function first defines a function called is_pangram that takes a string as input and returns a bool value. The function first initializes a variable called seen_letters to an array of 26 bool values, initialized to false. Then, the function iterates through the string, checking if each character is in the alphabet. If the character is in the alphabet, then the corresponding element in the seen_letters array is set to true. Finally, the function checks if all the elements in the seen_letters array are true. If they are, then the function returns true. Otherwise, the function returns false.
The main function of the program then calls the is_pangram function with the string "The quick brown fox jumps over the lazy dog" 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 is_pangram.rs and then compile it using the following command:
Code snippet
rustc is_pangram.rs
Once the program is compiled, you can run it using the following command:
Code snippet
./is_pangram
This will print the following output to the console:
Code snippet
The string The quick brown fox jumps over the lazy dog is a pangram
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 check if a given string is a pangram:
Rust
This function first defines a function called is_pangram that takes a string as input and returns a bool value. The function first initializes a variable called seen_letters to an array of 26 bool values, initialized to false. Then, the function iterates through the string, checking if each character is in the alphabet. If the character is in the alphabet, then the corresponding element in the seen_letters array is set to true. Finally, the function checks if all the elements in the seen_letters array are true. If they are, then the function returns true. Otherwise, the function returns false.
The main function of the program then calls the is_pangram function with the string "The quick brown fox jumps over the lazy dog" 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 is_pangram.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