---
title: "Write a Rust function to check if a given string is a pangram."  
description: "Write a Rust function to check if a given string is a pangram."  
author: "Utpal Vishwas"  
published: 2023-06-22  
updated: 2023-06-23  
canonical: https://www.mindstick.com/forum/158830/write-a-rust-function-to-check-if-a-given-string-is-a-pangram  
category: "rust"  
tags: ["string", "programs", "rust"]  
reading_time: 2 minutes  

---

# Write a Rust function to check if a given string is a pangram.

Write a [Rust](https://www.mindstick.com/forum/160170/explain-rust-s-ownership-borrowing-and-lifetimes-system) [function to check](https://www.mindstick.com/forum/161900/write-a-function-to-check-if-a-number-is-prime-with-explanation) if a given [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) is a pangram.

## Replies

### Reply by Aryan Kumar

Sure, here is a Rust [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a given string is a pangram:

Rust

```plaintext
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

```plaintext
rustc is_pangram.rs
```

Once the program is compiled, you can run it using the following command:

Code snippet

```plaintext
./is_pangram
```

This will print the following output to the console:

Code snippet

```plaintext
The string The quick brown fox jumps over the lazy dog is a pangram
```


---

Original Source: https://www.mindstick.com/forum/158830/write-a-rust-function-to-check-if-a-given-string-is-a-pangram

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
