Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use unicode_segmentation::UnicodeSegmentation;
- pub trait Palindrome {
- fn is_palindrome(&self) -> bool;
- }
- impl<N> Palindrome for [N]
- where N: Clone + PartialEq
- {
- fn is_palindrome(&self) -> bool {
- if self.is_empty() {
- true
- } else {
- let rev: Vec<N> = self.iter().map(|n| n.clone()).rev().collect();
- &rev == self
- }
- }
- }
- impl Palindrome for str {
- fn is_palindrome(&self) -> bool {
- let rev: String = self.graphemes(true).rev().collect();
- &rev == self
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement