aboutsummaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
Diffstat (limited to 'rust')
-rw-r--r--rust/acronym/.exercism/metadata.json1
-rw-r--r--rust/acronym/.gitignore8
-rw-r--r--rust/acronym/Cargo.toml6
-rw-r--r--rust/acronym/README.md88
-rw-r--r--rust/acronym/src/lib.rs19
-rw-r--r--rust/acronym/tests/acronym.rs73
6 files changed, 195 insertions, 0 deletions
diff --git a/rust/acronym/.exercism/metadata.json b/rust/acronym/.exercism/metadata.json
new file mode 100644
index 0000000..489e1a0
--- /dev/null
+++ b/rust/acronym/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"acronym","id":"c45e961dc43c4d01a07c60e31c75eb55","url":"https://exercism.io/my/solutions/c45e961dc43c4d01a07c60e31c75eb55","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/rust/acronym/.gitignore b/rust/acronym/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/rust/acronym/.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/rust/acronym/Cargo.toml b/rust/acronym/Cargo.toml
new file mode 100644
index 0000000..cc9c9d6
--- /dev/null
+++ b/rust/acronym/Cargo.toml
@@ -0,0 +1,6 @@
1[package]
2edition = "2018"
3name = "acronym"
4version = "1.7.0"
5
6[dependencies]
diff --git a/rust/acronym/README.md b/rust/acronym/README.md
new file mode 100644
index 0000000..8a73fdb
--- /dev/null
+++ b/rust/acronym/README.md
@@ -0,0 +1,88 @@
1# Acronym
2
3Convert a phrase to its acronym.
4
5Techies love their TLA (Three Letter Acronyms)!
6
7Help generate some jargon by writing a program that converts a long name
8like Portable Network Graphics to its acronym (PNG).
9
10## Rust Installation
11
12Refer to the [exercism help page][help-page] for Rust installation and learning
13resources.
14
15## Writing the Code
16
17Execute the tests with:
18
19```bash
20$ cargo test
21```
22
23All but the first test have been ignored. After you get the first test to
24pass, open the tests source file which is located in the `tests` directory
25and remove the `#[ignore]` flag from the next test and get the tests to pass
26again. Each separate test is a function with `#[test]` flag above it.
27Continue, until you pass every test.
28
29If you wish to run all ignored tests without editing the tests source file, use:
30
31```bash
32$ cargo test -- --ignored
33```
34
35To run a specific test, for example `some_test`, you can use:
36
37```bash
38$ cargo test some_test
39```
40
41If the specific test is ignored use:
42
43```bash
44$ cargo test some_test -- --ignored
45```
46
47To learn more about Rust tests refer to the [online test documentation][rust-tests]
48
49Make sure to read the [Modules][modules] chapter if you
50haven't already, it will help you with organizing your files.
51
52## Further improvements
53
54After 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.
55
56To format your solution, inside the solution directory use
57
58```bash
59cargo fmt
60```
61
62To see, if your solution contains some common ineffective use cases, inside the solution directory use
63
64```bash
65cargo clippy --all-targets
66```
67
68## Submitting the solution
69
70Generally 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.
71
72## Feedback, Issues, Pull Requests
73
74The [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!
75
76If 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).
77
78[help-page]: https://exercism.io/tracks/rust/learning
79[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
80[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
81[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
82
83## Source
84
85Julien Vanier [https://github.com/monkbroc](https://github.com/monkbroc)
86
87## Submitting Incomplete Solutions
88It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/rust/acronym/src/lib.rs b/rust/acronym/src/lib.rs
new file mode 100644
index 0000000..dcd4615
--- /dev/null
+++ b/rust/acronym/src/lib.rs
@@ -0,0 +1,19 @@
1fn to_acronym(word: &str) -> Option<String> {
2 if word.chars().all(|c| c.is_uppercase() || !c.is_alphabetic())
3 || word.chars().all(|c| c.is_lowercase() || !c.is_alphabetic())
4 {
5 word.chars()
6 .find(|c| c.is_alphabetic())
7 .map(|c| c.to_uppercase().to_string())
8 } else {
9 Some(word.chars().filter(|c| c.is_uppercase()).collect())
10 }
11}
12
13pub fn abbreviate(phrase: &str) -> String {
14 phrase
15 .split(|c| " -:,".contains(c)) // Split into words
16 .filter(|s| !s.is_empty())
17 .flat_map(to_acronym)
18 .collect()
19}
diff --git a/rust/acronym/tests/acronym.rs b/rust/acronym/tests/acronym.rs
new file mode 100644
index 0000000..079cf89
--- /dev/null
+++ b/rust/acronym/tests/acronym.rs
@@ -0,0 +1,73 @@
1#[test]
2fn empty() {
3 assert_eq!(acronym::abbreviate(""), "");
4}
5
6#[test]
7fn basic() {
8 assert_eq!(acronym::abbreviate("Portable Network Graphics"), "PNG");
9}
10
11#[test]
12fn lowercase_words() {
13 assert_eq!(acronym::abbreviate("Ruby on Rails"), "ROR");
14}
15
16#[test]
17fn camelcase() {
18 assert_eq!(acronym::abbreviate("HyperText Markup Language"), "HTML");
19}
20
21#[test]
22fn punctuation() {
23 assert_eq!(acronym::abbreviate("First In, First Out"), "FIFO");
24}
25
26#[test]
27fn all_caps_word() {
28 assert_eq!(
29 acronym::abbreviate("GNU Image Manipulation Program"),
30 "GIMP"
31 );
32}
33
34#[test]
35fn all_caps_word_with_punctuation() {
36 assert_eq!(acronym::abbreviate("PHP: Hypertext Preprocessor"), "PHP");
37}
38
39#[test]
40fn punctuation_without_whitespace() {
41 assert_eq!(
42 acronym::abbreviate("Complementary metal-oxide semiconductor"),
43 "CMOS"
44 );
45}
46
47#[test]
48fn very_long_abbreviation() {
49 assert_eq!(
50 acronym::abbreviate(
51 "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"
52 ),
53 "ROTFLSHTMDCOALM"
54 );
55}
56
57#[test]
58fn consecutive_delimiters() {
59 assert_eq!(
60 acronym::abbreviate("Something - I made up from thin air"),
61 "SIMUFTA"
62 );
63}
64
65#[test]
66fn apostrophes() {
67 assert_eq!(acronym::abbreviate("Halley's Comet"), "HC");
68}
69
70#[test]
71fn underscore_emphasis() {
72 assert_eq!(acronym::abbreviate("The Road _Not_ Taken"), "TRNT");
73}