Rust अभ्यास

Palindrome Check in Rust

Does the text read the same in both directions? Ignore spaces, punctuation and case.

  • शुरुआती
  • 2 मिनट में पढ़ें
  • 1 चलाने योग्य उदाहरण
आप क्या सीखेंगे
  • Filtering letters
  • Lower case
  • Comparing with the reverse
level
l0
e1
v2
e3
l4
The first and last letters match, then the next pair.

The same forwards and backwards

A palindrome reads the same in both directions, such as level. A good check ignores spaces, punctuation and the difference between upper and lower case.

filter keeps only letters and digits and map makes them lower case. eq compares the letters with the same letters in reverse order, without building a second vector. {text:?} prints the text with quotes.

मुख्य बातें

  1. Keep only letters and digits.
  2. Compare in lower case.
  3. eq compares two iterators item by item.

चलने योग्य Rust उदाहरण

Rust
कोड चलाएँ →
main.rs
fn is_palindrome(text: &str) -> bool {
    let letters: Vec<char> = text
        .chars()
        .filter(|c| c.is_alphanumeric())
        .map(|c| c.to_ascii_lowercase())
        .collect();
    letters.iter().eq(letters.iter().rev())
}

fn main() {
    for text in ["level", "A man, a plan, a canal: Panama", "Rust"] {
        println!("{text:?} -> {}", is_palindrome(text));
    }
}
इस उदाहरण के लिए Run बटन बनाएँ कोड चलाने का परिणाम
"level" -> true
"A man, a plan, a canal: Panama" -> true
"Rust" -> false

अभ्यास

उदाहरण को ऑनलाइन कंपाइलर में खोलें, कोई मान बदलें या एक पंक्ति जोड़ें, और चलाने से पहले आउटपुट का अनुमान लगाएँ। फिर कोड में जान-बूझकर गलती करें और कंपाइलर का संदेश पढ़ें।

यह पाठ पूरा कर लिया?

अपनी प्रगति ट्रैक करने के लिए इसे सीखा हुआ मार्क करें।

अगला: Word Frequency Counter →

चैलेंज से अभ्यास करें

इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।

सभी चैलेंज