Revolucent icon

Rust is_palindrome

Revolucent | PRO | 10/13/22 01:37:17 AM UTC (Edited) | 0 ⭐ | 9274 👁️ | Never ⏰ | []
Rust |

510 B

|

None

|

0 👍

/

0 👎

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
  }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎