Solve rust/reverse-string

This commit is contained in:
Avery Winters 2023-10-11 10:56:50 -05:00
parent 3823af76a5
commit 2a735f6868
Signed by: avery
SSH key fingerprint: SHA256:eesvLB5MMqHLZrAMFt6kEhqJWnASMLcET6Sgmw0FqZI
4 changed files with 9 additions and 8 deletions

View file

@ -35,6 +35,7 @@
{package = pkgs.exercism;} {package = pkgs.exercism;}
{package = pkgs.nil;} {package = pkgs.nil;}
{package = pkgs.rustc;} {package = pkgs.rustc;}
{package = pkgs.rustfmt;}
{package = pkgs.rust-analyzer;} {package = pkgs.rust-analyzer;}
]; ];
}; };

View file

@ -1,4 +1,5 @@
[dependencies] [dependencies]
unicode-segmentation = "1.10.1"
[features] [features]
grapheme = [] grapheme = []

View file

@ -1,3 +1,9 @@
use unicode_segmentation::UnicodeSegmentation;
pub fn reverse(input: &str) -> String { pub fn reverse(input: &str) -> String {
todo!("Write a function to reverse {input}"); if cfg!(feature = "grapheme") {
input.graphemes(true).rev().collect()
} else {
input.chars().rev().collect()
}
} }

View file

@ -19,49 +19,42 @@ fn an_empty_string() {
} }
#[test] #[test]
#[ignore]
/// a word /// a word
fn a_word() { fn a_word() {
process_reverse_case("robot", "tobor"); process_reverse_case("robot", "tobor");
} }
#[test] #[test]
#[ignore]
/// a capitalized word /// a capitalized word
fn a_capitalized_word() { fn a_capitalized_word() {
process_reverse_case("Ramen", "nemaR"); process_reverse_case("Ramen", "nemaR");
} }
#[test] #[test]
#[ignore]
/// a sentence with punctuation /// a sentence with punctuation
fn a_sentence_with_punctuation() { fn a_sentence_with_punctuation() {
process_reverse_case("I'm hungry!", "!yrgnuh m'I"); process_reverse_case("I'm hungry!", "!yrgnuh m'I");
} }
#[test] #[test]
#[ignore]
/// a palindrome /// a palindrome
fn a_palindrome() { fn a_palindrome() {
process_reverse_case("racecar", "racecar"); process_reverse_case("racecar", "racecar");
} }
#[test] #[test]
#[ignore]
/// an even-sized word /// an even-sized word
fn an_even_sized_word() { fn an_even_sized_word() {
process_reverse_case("drawer", "reward"); process_reverse_case("drawer", "reward");
} }
#[test] #[test]
#[ignore]
/// wide characters /// wide characters
fn wide_characters() { fn wide_characters() {
process_reverse_case("子猫", "猫子"); process_reverse_case("子猫", "猫子");
} }
#[test] #[test]
#[ignore]
#[cfg(feature = "grapheme")] #[cfg(feature = "grapheme")]
/// grapheme clusters /// grapheme clusters
fn grapheme_clusters() { fn grapheme_clusters() {