1: Add Rust track exercise "Hello World".
[?]
Aaw9nJhsNmfzFih9mKyNw9mV8CgERXJkRa1kK1Kx3LQH
Aug 15, 2021, 4:51 PM
FS63YMFJXLOXEDFKD5IETLB4XI6EUZOTRVHXPZVEZBPSFAQRZWNACDependencies
- [2]
265OXFLQ0: Add solutions for some of the exercises in experiment 1 "How does your experience affect how you code?".
Change contents
- file addition: hello-world[2.16]
- file addition: tests[0.23]
- file addition: hello-world.rs[0.41]
#[test]fn test_hello_world() {assert_eq!("Hello, World!", hello_world::hello());} - file addition: src[0.23]
- file addition: lib.rs[0.187]
// The &'static here means the return type has a static lifetime.// This is a Rust feature that you don't need to worry about now.pub fn hello() -> &'static str {"Goodbye, World!"} - file addition: README.md[0.23]
# Hello WorldThe classical introductory exercise. Just say "Hello, World!".["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) isthe traditional first program for beginning programming in a new languageor 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.## Rust InstallationRefer to the [exercism help page][help-page] for Rust installation and learningresources.## Writing the CodeExecute the tests with:```bash$ cargo test```All but the first test have been ignored. After you get the first test topass, open the tests source file which is located in the `tests` directoryand remove the `#[ignore]` flag from the next test and get the tests to passagain. 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 youhaven't already, it will help you with organizing your files.## Further improvementsAfter 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```bashcargo fmt```To see, if your solution contains some common ineffective use cases, inside the solution directory use```bashcargo clippy --all-targets```## Submitting the solutionGenerally 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 RequestsThe [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/main/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## SourceThis is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program)## Submitting Incomplete SolutionsIt's possible to submit an incomplete solution so you can see how others have completed the exercise. - file addition: GETTING_STARTED.md[0.23]
# Getting StartedThese exercises lean on Test-Driven Development (TDD), but they're notan 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 1Run the test suite. It can be run with `cargo`, which is installed with rust.```$ cargo test```This will compile the `hello-world` crate and run the test, which fails.```running 1 testtest test_hello_world ... FAILEDfailures:---- test_hello_world stdout ----thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`(left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5failures:test_hello_worldtest result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured```### Understanding Test FailuresThe `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.```---- 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 ErrorTo fix it, open up `src/lib.rs` and change the `hello` function to return`"Hello, World!"` instead of `"Goodbye, World!"`.```rustpub fn hello() -> &'static str {"Hello, World!"}```## Step 2Run the test again. This time, it will pass.```running 0 teststest result: ok. 0 passed; 0 failed; 0 ignored; 0 measuredRunning target/debug/deps/hello_world-bd1f06dc726ef14frunning 1 testtest test_hello_world ... oktest result: ok. 1 passed; 0 failed; 0 ignored; 0 measuredDoc-tests hello-worldrunning 0 teststest result: ok. 0 passed; 0 failed; 0 ignored; 0 measured```## SubmitOnce the test is passing, you can submit your code with the followingcommand:```$ exercism submit src/lib.rs``` - file addition: Cargo.toml[0.23]
[package]edition = "2018"name = "hello-world"version = "1.1.0" - file addition: .gitignore[0.23]
# 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-cargolockCargo.lock - file addition: .exercism[0.23]
- file addition: metadata.json[0.6468]
{"track":"rust","exercise":"hello-world","id":"e929606c333e4f579e3b4ef05be665a5","url":"https://exercism.io/my/solutions/e929606c333e4f579e3b4ef05be665a5","handle":"nicoty","is_requester":true,"auto_approve":true}