From 2a735f68683143c368a9da418518243f9c10b47a Mon Sep 17 00:00:00 2001 From: Avery Winters Date: Wed, 11 Oct 2023 10:56:50 -0500 Subject: [PATCH] Solve rust/reverse-string --- flake.nix | 1 + rust/reverse-string/Cargo.toml | 1 + rust/reverse-string/src/lib.rs | 8 +++++++- rust/reverse-string/tests/reverse-string.rs | 7 ------- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/flake.nix b/flake.nix index c837204..5c73ca1 100644 --- a/flake.nix +++ b/flake.nix @@ -35,6 +35,7 @@ {package = pkgs.exercism;} {package = pkgs.nil;} {package = pkgs.rustc;} + {package = pkgs.rustfmt;} {package = pkgs.rust-analyzer;} ]; }; diff --git a/rust/reverse-string/Cargo.toml b/rust/reverse-string/Cargo.toml index 52b902c..1f894d5 100644 --- a/rust/reverse-string/Cargo.toml +++ b/rust/reverse-string/Cargo.toml @@ -1,4 +1,5 @@ [dependencies] +unicode-segmentation = "1.10.1" [features] grapheme = [] diff --git a/rust/reverse-string/src/lib.rs b/rust/reverse-string/src/lib.rs index 01e0d4d..22722c2 100644 --- a/rust/reverse-string/src/lib.rs +++ b/rust/reverse-string/src/lib.rs @@ -1,3 +1,9 @@ +use unicode_segmentation::UnicodeSegmentation; + 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() + } } diff --git a/rust/reverse-string/tests/reverse-string.rs b/rust/reverse-string/tests/reverse-string.rs index b80eef3..e6ea31c 100644 --- a/rust/reverse-string/tests/reverse-string.rs +++ b/rust/reverse-string/tests/reverse-string.rs @@ -19,49 +19,42 @@ fn an_empty_string() { } #[test] -#[ignore] /// a word fn a_word() { process_reverse_case("robot", "tobor"); } #[test] -#[ignore] /// a capitalized word fn a_capitalized_word() { process_reverse_case("Ramen", "nemaR"); } #[test] -#[ignore] /// a sentence with punctuation fn a_sentence_with_punctuation() { process_reverse_case("I'm hungry!", "!yrgnuh m'I"); } #[test] -#[ignore] /// a palindrome fn a_palindrome() { process_reverse_case("racecar", "racecar"); } #[test] -#[ignore] /// an even-sized word fn an_even_sized_word() { process_reverse_case("drawer", "reward"); } #[test] -#[ignore] /// wide characters fn wide_characters() { process_reverse_case("子猫", "猫子"); } #[test] -#[ignore] #[cfg(feature = "grapheme")] /// grapheme clusters fn grapheme_clusters() {