From 852758eb9c2ad36cfc9aa187ec4a1fe90ccfd11a Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Mon, 28 Oct 2024 22:22:16 -0400 Subject: [PATCH] Rust: completed Reverse String again --- rust/reverse-string/.exercism/metadata.json | 2 +- rust/reverse-string/Cargo.toml | 11 ++++++----- rust/reverse-string/README.md | 15 +++++++++------ rust/reverse-string/src/lib.rs | 4 +++- rust/reverse-string/tests/reverse-string.rs | 8 ++++++-- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/rust/reverse-string/.exercism/metadata.json b/rust/reverse-string/.exercism/metadata.json index f306970..825a0fa 100644 --- a/rust/reverse-string/.exercism/metadata.json +++ b/rust/reverse-string/.exercism/metadata.json @@ -1 +1 @@ -{"track":"rust","exercise":"reverse-string","id":"aeec5dd024f94c60b89e66907f4e90b9","url":"https://exercism.org/tracks/rust/exercises/reverse-string","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file +{"track":"rust","exercise":"reverse-string","id":"f97879b6def647a5bf82751034c483e3","url":"https://exercism.org/tracks/rust/exercises/reverse-string","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/rust/reverse-string/Cargo.toml b/rust/reverse-string/Cargo.toml index 52b902c..e8a1ce6 100644 --- a/rust/reverse-string/Cargo.toml +++ b/rust/reverse-string/Cargo.toml @@ -1,9 +1,10 @@ -[dependencies] - -[features] -grapheme = [] - [package] edition = "2021" name = "reverse_string" version = "1.2.0" + +[features] +grapheme = [] + +[dependencies] +unicode-segmentation = "=1.11.0" diff --git a/rust/reverse-string/README.md b/rust/reverse-string/README.md index afd18f8..ca34435 100644 --- a/rust/reverse-string/README.md +++ b/rust/reverse-string/README.md @@ -21,17 +21,20 @@ Some examples: ## Bonus -Test your function on this string: `uüu` and see what happens. Try to write a function that properly -reverses this string. Hint: grapheme clusters +Test your function on this string: `uüu` and see what happens. +Try to write a function that properly reverses this string. +Hint: grapheme clusters -To get the bonus test to run, remove the ignore flag (`#[ignore]`) from the -last test, and execute the tests with: +To get the bonus test to run, remove the ignore flag (`#[ignore]`) from the last test, and execute the tests with: ```bash -$ cargo test --features grapheme +cargo test --features grapheme ``` -You will need to use external libraries (a `crate` in rust lingo) for the bonus task. A good place to look for those is [crates.io](https://crates.io/), the official repository of crates. +You will need to use external libraries (a `crate` in rust lingo) for the bonus task. +A good place to look for those is [crates.io](https://crates.io/), the official repository of crates. +Please remember that only a limited set of crates is supported by our test runner. +The full list is in [this file](https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml) under the section `[dependencies]`. [Check the documentation](https://doc.rust-lang.org/cargo/guide/dependencies.html) for instructions on how to use external crates in your projects. diff --git a/rust/reverse-string/src/lib.rs b/rust/reverse-string/src/lib.rs index 0393f10..1c19104 100644 --- a/rust/reverse-string/src/lib.rs +++ b/rust/reverse-string/src/lib.rs @@ -1,3 +1,5 @@ +use unicode_segmentation::UnicodeSegmentation; + pub fn reverse(input: &str) -> String { - input.chars().rev().collect() + input.graphemes(true).rev().collect() } diff --git a/rust/reverse-string/tests/reverse-string.rs b/rust/reverse-string/tests/reverse-string.rs index cc16866..06f9263 100644 --- a/rust/reverse-string/tests/reverse-string.rs +++ b/rust/reverse-string/tests/reverse-string.rs @@ -9,6 +9,7 @@ fn an_empty_string() { } #[test] + fn a_word() { let input = "robot"; let output = reverse(input); @@ -17,6 +18,7 @@ fn a_word() { } #[test] + fn a_capitalized_word() { let input = "Ramen"; let output = reverse(input); @@ -25,6 +27,7 @@ fn a_capitalized_word() { } #[test] + fn a_sentence_with_punctuation() { let input = "I'm hungry!"; let output = reverse(input); @@ -33,6 +36,7 @@ fn a_sentence_with_punctuation() { } #[test] + fn a_palindrome() { let input = "racecar"; let output = reverse(input); @@ -41,6 +45,7 @@ fn a_palindrome() { } #[test] + fn an_even_sized_word() { let input = "drawer"; let output = reverse(input); @@ -49,6 +54,7 @@ fn an_even_sized_word() { } #[test] + fn wide_characters() { let input = "子猫"; let output = reverse(input); @@ -57,7 +63,6 @@ fn wide_characters() { } #[test] -#[ignore] #[cfg(feature = "grapheme")] fn grapheme_cluster_with_pre_combined_form() { let input = "Würstchenstand"; @@ -67,7 +72,6 @@ fn grapheme_cluster_with_pre_combined_form() { } #[test] -#[ignore] #[cfg(feature = "grapheme")] fn grapheme_clusters() { let input = "ผู้เขียนโปรแกรม";