From d32049181dccf124b35f8dd063395ac4a0203a91 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 27 Sep 2021 08:24:35 +0200 Subject: [PATCH] Exercism --- .../rust/hello-world/.exercism/config.json | 41 +++++++++ .../rust/hello-world/.exercism/metadata.json | 1 + prog2/exercism/rust/hello-world/.gitignore | 8 ++ prog2/exercism/rust/hello-world/Cargo.toml | 4 + .../rust/hello-world/GETTING_STARTED.md | 92 +++++++++++++++++++ prog2/exercism/rust/hello-world/HELP.md | 85 +++++++++++++++++ prog2/exercism/rust/hello-world/README.md | 53 +++++++++++ prog2/exercism/rust/hello-world/src/lib.rs | 4 + .../rust/hello-world/tests/hello-world.rs | 4 + 9 files changed, 292 insertions(+) create mode 100644 prog2/exercism/rust/hello-world/.exercism/config.json create mode 100644 prog2/exercism/rust/hello-world/.exercism/metadata.json create mode 100644 prog2/exercism/rust/hello-world/.gitignore create mode 100644 prog2/exercism/rust/hello-world/Cargo.toml create mode 100644 prog2/exercism/rust/hello-world/GETTING_STARTED.md create mode 100644 prog2/exercism/rust/hello-world/HELP.md create mode 100644 prog2/exercism/rust/hello-world/README.md create mode 100644 prog2/exercism/rust/hello-world/src/lib.rs create mode 100644 prog2/exercism/rust/hello-world/tests/hello-world.rs diff --git a/prog2/exercism/rust/hello-world/.exercism/config.json b/prog2/exercism/rust/hello-world/.exercism/config.json new file mode 100644 index 0000000..91dae99 --- /dev/null +++ b/prog2/exercism/rust/hello-world/.exercism/config.json @@ -0,0 +1,41 @@ +{ + "blurb": "The classical introductory exercise. Just say \"Hello, World!\"", + "authors": [ + "EduardoBautista" + ], + "contributors": [ + "ashleygwilliams", + "ClashTheBunny", + "coriolinus", + "cwhakes", + "dvoytik", + "EduardoBautista", + "efx", + "ErikSchierboom", + "hydhknn", + "IanWhitney", + "ijanos", + "kytrinyx", + "lutostag", + "nfiles", + "petertseng", + "regnerjr", + "rofrol", + "stringparser", + "xakon", + "ZapAnton" + ], + "files": { + "solution": [ + "src/lib.rs" + ], + "test": [ + "tests/hello-world.rs" + ], + "example": [ + ".meta/example.rs" + ] + }, + "source": "This is an exercise to introduce users to using Exercism", + "source_url": "http://en.wikipedia.org/wiki/%22Hello,_world!%22_program" +} diff --git a/prog2/exercism/rust/hello-world/.exercism/metadata.json b/prog2/exercism/rust/hello-world/.exercism/metadata.json new file mode 100644 index 0000000..eaab340 --- /dev/null +++ b/prog2/exercism/rust/hello-world/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"hello-world","id":"dbd555e599224cc7b75b1eaf37f68c6e","url":"https://exercism.org/tracks/rust/exercises/hello-world","handle":"E-Almqvist","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/prog2/exercism/rust/hello-world/.gitignore b/prog2/exercism/rust/hello-world/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/prog2/exercism/rust/hello-world/.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/prog2/exercism/rust/hello-world/Cargo.toml b/prog2/exercism/rust/hello-world/Cargo.toml new file mode 100644 index 0000000..4d9f8bb --- /dev/null +++ b/prog2/exercism/rust/hello-world/Cargo.toml @@ -0,0 +1,4 @@ +[package] +edition = "2018" +name = "hello-world" +version = "1.1.0" diff --git a/prog2/exercism/rust/hello-world/GETTING_STARTED.md b/prog2/exercism/rust/hello-world/GETTING_STARTED.md new file mode 100644 index 0000000..04ec562 --- /dev/null +++ b/prog2/exercism/rust/hello-world/GETTING_STARTED.md @@ -0,0 +1,92 @@ +# Getting Started + +These exercises lean on Test-Driven Development (TDD), but they're not +an exact match. + +The following steps assume that you are in the same directory as the exercise. + +You must have Rust installed. +Follow the [Installation chapter in the Rust book](https://doc.rust-lang.org/book/ch01-01-installation.html). +The [Rust language section](http://exercism.io/languages/rust) +section from exercism is also useful. + +## Step 1 + +Run the test suite. It can be run with `cargo`, which is installed with Rust. + +```sh +$ cargo test +``` + +This will compile the `hello-world` crate and run the test, which fails. + +```sh +running 1 test +test test_hello_world ... FAILED + +failures: + +---- test_hello_world stdout ---- +thread 'test_hello_world' panicked at 'assertion failed: `(left == right)` +(left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5 + +failures: + test_hello_world + +test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured +``` + +### Understanding Test Failures + +The `test_hello_world` failure states that it is expecting the value, +`"Hello, World!"`, to be returned from `hello()`. +The left side of the assertion (at line 5) should be equal to the right side. + +```sh +---- test_hello_world stdout ---- +thread 'test_hello_world' panicked at 'assertion failed: `(left == right)` +(left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5 +``` + +### Fixing the Error + +To fix it, open up `src/lib.rs` and change the `hello` function to return +`"Hello, World!"` instead of `"Goodbye, World!"`. + +```rust +pub fn hello() -> &'static str { + "Hello, World!" +} +``` + +## Step 2 + +Run the test again. This time, it will pass. + +```sh +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured + + Running target/debug/deps/hello_world-bd1f06dc726ef14f + +running 1 test +test test_hello_world ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured + + Doc-tests hello-world + +running 0 tests + +test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured +``` + +## Submit + +Once the test is passing, you can submit your code with the following +command: + +```sh +$ exercism submit src/lib.rs +``` diff --git a/prog2/exercism/rust/hello-world/HELP.md b/prog2/exercism/rust/hello-world/HELP.md new file mode 100644 index 0000000..b4252f8 --- /dev/null +++ b/prog2/exercism/rust/hello-world/HELP.md @@ -0,0 +1,85 @@ +# Help + +## Running the tests + +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 _only ignored_ tests without editing the tests source file, use: + +```bash +$ cargo test -- --ignored +``` + +If you are using Rust 1.51 or later, you can run _all_ tests with + +```bash +$ cargo test -- --include-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]. + +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html + +## Submitting your solution + +You can submit your solution using the `exercism submit src/lib.rs` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Rust track's documentation](https://exercism.org/docs/tracks/rust) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## 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 GitHub [track repository][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]. + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + +[help-page]: https://exercism.io/tracks/rust/learning +[github]: https://github.com/exercism/rust +[contribution guide]: https://exercism.io/docs/community/contributors \ No newline at end of file diff --git a/prog2/exercism/rust/hello-world/README.md b/prog2/exercism/rust/hello-world/README.md new file mode 100644 index 0000000..4ece0fa --- /dev/null +++ b/prog2/exercism/rust/hello-world/README.md @@ -0,0 +1,53 @@ +# Hello World + +Welcome to Hello World on Exercism's Rust Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +The classical introductory exercise. Just say "Hello, World!". + +["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is +the traditional first program for beginning programming in a new language +or environment. + +The objectives are simple: + +- Write a function that returns the string "Hello, World!". +- Run the test suite and make sure that it succeeds. +- Submit your solution and check it at the website. + +If everything goes well, you will be ready to fetch your first real exercise. + +## Source + +### Created by + +- @EduardoBautista + +### Contributed to by + +- @ashleygwilliams +- @ClashTheBunny +- @coriolinus +- @cwhakes +- @dvoytik +- @EduardoBautista +- @efx +- @ErikSchierboom +- @hydhknn +- @IanWhitney +- @ijanos +- @kytrinyx +- @lutostag +- @nfiles +- @petertseng +- @regnerjr +- @rofrol +- @stringparser +- @xakon +- @ZapAnton + +### Based on + +This is an exercise to introduce users to using Exercism - http://en.wikipedia.org/wiki/%22Hello,_world!%22_program \ No newline at end of file diff --git a/prog2/exercism/rust/hello-world/src/lib.rs b/prog2/exercism/rust/hello-world/src/lib.rs new file mode 100644 index 0000000..05f6f4a --- /dev/null +++ b/prog2/exercism/rust/hello-world/src/lib.rs @@ -0,0 +1,4 @@ +// &'static is a "lifetime specifier", something you'll learn more about later +pub fn hello() -> &'static str { + "Goodbye, Mars!" +} diff --git a/prog2/exercism/rust/hello-world/tests/hello-world.rs b/prog2/exercism/rust/hello-world/tests/hello-world.rs new file mode 100644 index 0000000..b29f953 --- /dev/null +++ b/prog2/exercism/rust/hello-world/tests/hello-world.rs @@ -0,0 +1,4 @@ +#[test] +fn test_hello_world() { + assert_eq!("Hello, World!", hello_world::hello()); +}