aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--reverse-string/.exercism/metadata.json1
-rw-r--r--reverse-string/.gitignore8
-rw-r--r--reverse-string/Cargo.toml10
-rw-r--r--reverse-string/README.md103
-rw-r--r--reverse-string/src/lib.rs5
-rw-r--r--reverse-string/tests/reverse-string.rs62
6 files changed, 189 insertions, 0 deletions
diff --git a/reverse-string/.exercism/metadata.json b/reverse-string/.exercism/metadata.json
new file mode 100644
index 0000000..667d0ed
--- /dev/null
+++ b/reverse-string/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"reverse-string","id":"f735abfd7ff5444fb0f6c68c5fbef1b9","url":"https://exercism.io/my/solutions/f735abfd7ff5444fb0f6c68c5fbef1b9","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/reverse-string/.gitignore b/reverse-string/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/reverse-string/.gitignore
@@ -0,0 +1,8 @@
1# Generated by Cargo
2# will have compiled files and executables
3/target/
4**/*.rs.bk
5
6# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8Cargo.lock
diff --git a/reverse-string/Cargo.toml b/reverse-string/Cargo.toml
new file mode 100644
index 0000000..bdf501c
--- /dev/null
+++ b/reverse-string/Cargo.toml
@@ -0,0 +1,10 @@
1[dependencies]
2unicode-segmentation = "1.3.0"
3
4[features]
5grapheme = []
6
7[package]
8edition = "2018"
9name = "reverse_string"
10version = "1.2.0"
diff --git a/reverse-string/README.md b/reverse-string/README.md
new file mode 100644
index 0000000..78e9753
--- /dev/null
+++ b/reverse-string/README.md
@@ -0,0 +1,103 @@
1# Reverse String
2
3Reverse a string
4
5For example:
6input: "cool"
7output: "looc"
8
9## Bonus
10Test your function on this string: `uüu` and see what happens. Try to write a function that properly
11reverses this string. Hint: grapheme clusters
12
13To get the bonus test to run, remove the ignore flag (`#[ignore]`) from the
14last test, and execute the tests with:
15
16```bash
17$ cargo test --features grapheme
18```
19
20You 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.
21
22[Check the documentation](https://doc.rust-lang.org/cargo/guide/dependencies.html) for instructions on how to use external crates in your projects.
23
24
25## Rust Installation
26
27Refer to the [exercism help page][help-page] for Rust installation and learning
28resources.
29
30## Writing the Code
31
32Execute the tests with:
33
34```bash
35$ cargo test
36```
37
38All but the first test have been ignored. After you get the first test to
39pass, open the tests source file which is located in the `tests` directory
40and remove the `#[ignore]` flag from the next test and get the tests to pass
41again. Each separate test is a function with `#[test]` flag above it.
42Continue, until you pass every test.
43
44If you wish to run all ignored tests without editing the tests source file, use:
45
46```bash
47$ cargo test -- --ignored
48```
49
50To run a specific test, for example `some_test`, you can use:
51
52```bash
53$ cargo test some_test
54```
55
56If the specific test is ignored use:
57
58```bash
59$ cargo test some_test -- --ignored
60```
61
62To learn more about Rust tests refer to the [online test documentation][rust-tests]
63
64Make sure to read the [Modules][modules] chapter if you
65haven't already, it will help you with organizing your files.
66
67## Further improvements
68
69After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
70
71To format your solution, inside the solution directory use
72
73```bash
74cargo fmt
75```
76
77To see, if your solution contains some common ineffective use cases, inside the solution directory use
78
79```bash
80cargo clippy --all-targets
81```
82
83## Submitting the solution
84
85Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
86
87## Feedback, Issues, Pull Requests
88
89The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
90
91If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
92
93[help-page]: https://exercism.io/tracks/rust/learning
94[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
95[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
96[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
97
98## Source
99
100Introductory challenge to reverse an input string [https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb](https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb)
101
102## Submitting Incomplete Solutions
103It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/reverse-string/src/lib.rs b/reverse-string/src/lib.rs
new file mode 100644
index 0000000..f488a6e
--- /dev/null
+++ b/reverse-string/src/lib.rs
@@ -0,0 +1,5 @@
1use unicode_segmentation::UnicodeSegmentation;
2
3pub fn reverse(input: &str) -> String {
4 UnicodeSegmentation::graphemes(input, true).rev().collect()
5}
diff --git a/reverse-string/tests/reverse-string.rs b/reverse-string/tests/reverse-string.rs
new file mode 100644
index 0000000..9b17bb0
--- /dev/null
+++ b/reverse-string/tests/reverse-string.rs
@@ -0,0 +1,62 @@
1//! Tests for reverse-string
2//!
3//! Generated by [script][script] using [canonical data][canonical-data]
4//!
5//! [script]: https://github.com/exercism/rust/blob/b829ce2/bin/init_exercise.py
6//! [canonical-data]: https://raw.githubusercontent.com/exercism/problem-specifications/master/exercises/reverse-string/canonical_data.json
7
8use reverse_string::*;
9
10/// Process a single test case for the property `reverse`
11fn process_reverse_case(input: &str, expected: &str) {
12 assert_eq!(&reverse(input), expected)
13}
14
15#[test]
16/// empty string
17fn test_an_empty_string() {
18 process_reverse_case("", "");
19}
20
21#[test]
22/// a word
23fn test_a_word() {
24 process_reverse_case("robot", "tobor");
25}
26
27#[test]
28/// a capitalized word
29fn test_a_capitalized_word() {
30 process_reverse_case("Ramen", "nemaR");
31}
32
33#[test]
34/// a sentence with punctuation
35fn test_a_sentence_with_punctuation() {
36 process_reverse_case("I'm hungry!", "!yrgnuh m'I");
37}
38
39#[test]
40/// a palindrome
41fn test_a_palindrome() {
42 process_reverse_case("racecar", "racecar");
43}
44
45#[test]
46/// an even-sized word
47fn test_an_even_sized_word() {
48 process_reverse_case("drawer", "reward");
49}
50
51#[test]
52/// wide characters
53fn test_wide_characters() {
54 process_reverse_case("子猫", "猫子");
55}
56
57#[test]
58#[cfg(feature = "grapheme")]
59/// grapheme clusters
60fn test_grapheme_clusters() {
61 process_reverse_case("uüu", "uüu");
62}