From 02481656966b0a8dfc95cf3c22bcc049660ff7d4 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sat, 26 Dec 2020 17:48:38 +0000 Subject: Move Rust exercises in a subdirectory --- rust/proverb/.exercism/metadata.json | 1 + rust/proverb/.gitignore | 8 +++ rust/proverb/Cargo.toml | 6 +++ rust/proverb/README.md | 97 ++++++++++++++++++++++++++++++++++++ rust/proverb/src/lib.rs | 15 ++++++ rust/proverb/tests/proverb.rs | 70 ++++++++++++++++++++++++++ 6 files changed, 197 insertions(+) create mode 100644 rust/proverb/.exercism/metadata.json create mode 100644 rust/proverb/.gitignore create mode 100644 rust/proverb/Cargo.toml create mode 100644 rust/proverb/README.md create mode 100644 rust/proverb/src/lib.rs create mode 100644 rust/proverb/tests/proverb.rs (limited to 'rust/proverb') diff --git a/rust/proverb/.exercism/metadata.json b/rust/proverb/.exercism/metadata.json new file mode 100644 index 0000000..92c8e56 --- /dev/null +++ b/rust/proverb/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"proverb","id":"df96008ca5da43758fca9ac1ee80607e","url":"https://exercism.io/my/solutions/df96008ca5da43758fca9ac1ee80607e","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/rust/proverb/.gitignore b/rust/proverb/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/rust/proverb/.gitignore @@ -0,0 +1,8 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ +**/*.rs.bk + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock +Cargo.lock diff --git a/rust/proverb/Cargo.toml b/rust/proverb/Cargo.toml new file mode 100644 index 0000000..4e09ea1 --- /dev/null +++ b/rust/proverb/Cargo.toml @@ -0,0 +1,6 @@ +[package] +edition = "2018" +name = "proverb" +version = "1.1.0" + +[dependencies] diff --git a/rust/proverb/README.md b/rust/proverb/README.md new file mode 100644 index 0000000..b9efe0b --- /dev/null +++ b/rust/proverb/README.md @@ -0,0 +1,97 @@ +# Proverb + +For want of a horseshoe nail, a kingdom was lost, or so the saying goes. + +Given a list of inputs, generate the relevant proverb. For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme: + +```text +For want of a nail the shoe was lost. +For want of a shoe the horse was lost. +For want of a horse the rider was lost. +For want of a rider the message was lost. +For want of a message the battle was lost. +For want of a battle the kingdom was lost. +And all for the want of a nail. +``` + +Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. No line of the output text should be a static, unchanging string; all should vary according to the input given. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Writing the Code + +Execute the tests with: + +```bash +$ cargo test +``` + +All but the first test have been ignored. After you get the first test to +pass, open the tests source file which is located in the `tests` directory +and remove the `#[ignore]` flag from the next test and get the tests to pass +again. Each separate test is a function with `#[test]` flag above it. +Continue, until you pass every test. + +If you wish to run all ignored tests without editing the tests source file, use: + +```bash +$ cargo test -- --ignored +``` + +To run a specific test, for example `some_test`, you can use: + +```bash +$ cargo test some_test +``` + +If the specific test is ignored use: + +```bash +$ cargo test some_test -- --ignored +``` + +To learn more about Rust tests refer to the [online test documentation][rust-tests] + +Make sure to read the [Modules][modules] chapter if you +haven't already, it will help you with organizing your files. + +## Further improvements + +After 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. + +To format your solution, inside the solution directory use + +```bash +cargo fmt +``` + +To see, if your solution contains some common ineffective use cases, inside the solution directory use + +```bash +cargo clippy --all-targets +``` + +## Submitting the solution + +Generally 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. + +## Feedback, Issues, Pull Requests + +The [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! + +If 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). + +[help-page]: https://exercism.io/tracks/rust/learning +[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html +[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html + +## Source + +Wikipedia [http://en.wikipedia.org/wiki/For_Want_of_a_Nail](http://en.wikipedia.org/wiki/For_Want_of_a_Nail) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/rust/proverb/src/lib.rs b/rust/proverb/src/lib.rs new file mode 100644 index 0000000..1046553 --- /dev/null +++ b/rust/proverb/src/lib.rs @@ -0,0 +1,15 @@ +use std::iter; + +pub fn build_proverb(list: &[&str]) -> String { + if list.is_empty() { + String::new() + } else { + let reason = |(a, b)| format!("For want of a {} the {} was lost.\n", a, b); + let bitter_end = format!("And all for the want of a {}.", list[0]); + list.iter() + .zip(list.iter().skip(1)) // .window(2) also works here + .map(reason) + .chain(iter::once(bitter_end)) + .collect() + } +} diff --git a/rust/proverb/tests/proverb.rs b/rust/proverb/tests/proverb.rs new file mode 100644 index 0000000..220de45 --- /dev/null +++ b/rust/proverb/tests/proverb.rs @@ -0,0 +1,70 @@ +use proverb::build_proverb; + +#[test] +fn test_two_pieces() { + let input = vec!["nail", "shoe"]; + let expected = vec![ + "For want of a nail the shoe was lost.", + "And all for the want of a nail.", + ] + .join("\n"); + assert_eq!(build_proverb(&input), expected); +} + +// Notice the change in the last line at three pieces. +#[test] +fn test_three_pieces() { + let input = vec!["nail", "shoe", "horse"]; + let expected = vec![ + "For want of a nail the shoe was lost.", + "For want of a shoe the horse was lost.", + "And all for the want of a nail.", + ] + .join("\n"); + assert_eq!(build_proverb(&input), expected); +} + +#[test] +fn test_one_piece() { + let input = vec!["nail"]; + let expected = String::from("And all for the want of a nail."); + assert_eq!(build_proverb(&input), expected); +} + +#[test] +fn test_zero_pieces() { + let input: Vec<&str> = vec![]; + let expected = String::new(); + assert_eq!(build_proverb(&input), expected); +} + +#[test] +fn test_full() { + let input = vec![ + "nail", "shoe", "horse", "rider", "message", "battle", "kingdom", + ]; + let expected = vec![ + "For want of a nail the shoe was lost.", + "For want of a shoe the horse was lost.", + "For want of a horse the rider was lost.", + "For want of a rider the message was lost.", + "For want of a message the battle was lost.", + "For want of a battle the kingdom was lost.", + "And all for the want of a nail.", + ] + .join("\n"); + assert_eq!(build_proverb(&input), expected); +} + +#[test] +fn test_three_pieces_modernized() { + let input = vec!["pin", "gun", "soldier", "battle"]; + let expected = vec![ + "For want of a pin the gun was lost.", + "For want of a gun the soldier was lost.", + "For want of a soldier the battle was lost.", + "And all for the want of a pin.", + ] + .join("\n"); + assert_eq!(build_proverb(&input), expected); +} -- cgit v1.2.3