Rust: completed Reverse String again

This commit is contained in:
Andrew Scott 2024-10-28 22:22:16 -04:00
parent 59a1cb14fa
commit 852758eb9c
Signed by: a
GPG key ID: 7CD5A5977E4931C1
5 changed files with 25 additions and 15 deletions

View file

@ -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} {"track":"rust","exercise":"reverse-string","id":"f97879b6def647a5bf82751034c483e3","url":"https://exercism.org/tracks/rust/exercises/reverse-string","handle":"Chomp1295","is_requester":true,"auto_approve":false}

View file

@ -1,9 +1,10 @@
[dependencies]
[features]
grapheme = []
[package] [package]
edition = "2021" edition = "2021"
name = "reverse_string" name = "reverse_string"
version = "1.2.0" version = "1.2.0"
[features]
grapheme = []
[dependencies]
unicode-segmentation = "=1.11.0"

View file

@ -21,17 +21,20 @@ Some examples:
## Bonus ## Bonus
Test your function on this string: `uüu` and see what happens. Try to write a function that properly Test your function on this string: `uüu` and see what happens.
reverses this string. Hint: grapheme clusters 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 To get the bonus test to run, remove the ignore flag (`#[ignore]`) from the last test, and execute the tests with:
last test, and execute the tests with:
```bash ```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. [Check the documentation](https://doc.rust-lang.org/cargo/guide/dependencies.html) for instructions on how to use external crates in your projects.

View file

@ -1,3 +1,5 @@
use unicode_segmentation::UnicodeSegmentation;
pub fn reverse(input: &str) -> String { pub fn reverse(input: &str) -> String {
input.chars().rev().collect() input.graphemes(true).rev().collect()
} }

View file

@ -9,6 +9,7 @@ fn an_empty_string() {
} }
#[test] #[test]
fn a_word() { fn a_word() {
let input = "robot"; let input = "robot";
let output = reverse(input); let output = reverse(input);
@ -17,6 +18,7 @@ fn a_word() {
} }
#[test] #[test]
fn a_capitalized_word() { fn a_capitalized_word() {
let input = "Ramen"; let input = "Ramen";
let output = reverse(input); let output = reverse(input);
@ -25,6 +27,7 @@ fn a_capitalized_word() {
} }
#[test] #[test]
fn a_sentence_with_punctuation() { fn a_sentence_with_punctuation() {
let input = "I'm hungry!"; let input = "I'm hungry!";
let output = reverse(input); let output = reverse(input);
@ -33,6 +36,7 @@ fn a_sentence_with_punctuation() {
} }
#[test] #[test]
fn a_palindrome() { fn a_palindrome() {
let input = "racecar"; let input = "racecar";
let output = reverse(input); let output = reverse(input);
@ -41,6 +45,7 @@ fn a_palindrome() {
} }
#[test] #[test]
fn an_even_sized_word() { fn an_even_sized_word() {
let input = "drawer"; let input = "drawer";
let output = reverse(input); let output = reverse(input);
@ -49,6 +54,7 @@ fn an_even_sized_word() {
} }
#[test] #[test]
fn wide_characters() { fn wide_characters() {
let input = "子猫"; let input = "子猫";
let output = reverse(input); let output = reverse(input);
@ -57,7 +63,6 @@ fn wide_characters() {
} }
#[test] #[test]
#[ignore]
#[cfg(feature = "grapheme")] #[cfg(feature = "grapheme")]
fn grapheme_cluster_with_pre_combined_form() { fn grapheme_cluster_with_pre_combined_form() {
let input = "Würstchenstand"; let input = "Würstchenstand";
@ -67,7 +72,6 @@ fn grapheme_cluster_with_pre_combined_form() {
} }
#[test] #[test]
#[ignore]
#[cfg(feature = "grapheme")] #[cfg(feature = "grapheme")]
fn grapheme_clusters() { fn grapheme_clusters() {
let input = "ผู้เขียนโปรแกรม"; let input = "ผู้เขียนโปรแกรม";