Exercism: Rust version of the 'Hello World' exercise.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.1 KiB

3 years ago
  1. # Getting Started
  2. These exercises lean on Test-Driven Development (TDD), but they're not
  3. an exact match.
  4. The following steps assume that you are in the same directory as the exercise.
  5. You must have rust installed.
  6. Follow the [Installation chapter in the Rust book](https://doc.rust-lang.org/book/ch01-01-installation.html).
  7. The [Rust language section](http://exercism.io/languages/rust)
  8. section from exercism is also useful.
  9. ## Step 1
  10. Run the test suite. It can be run with `cargo`, which is installed with rust.
  11. ```
  12. $ cargo test
  13. ```
  14. This will compile the `hello-world` crate and run the test, which fails.
  15. ```
  16. running 1 test
  17. test test_hello_world ... FAILED
  18. failures:
  19. ---- test_hello_world stdout ----
  20. thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`
  21. (left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5
  22. failures:
  23. test_hello_world
  24. test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
  25. ```
  26. ### Understanding Test Failures
  27. The `test_hello_world` failure states that it is expecting the value,
  28. `"Hello, World!"`, to be returned from `hello()`.
  29. The left side of the assertion (at line 5) should be equal to the right side.
  30. ```
  31. ---- test_hello_world stdout ----
  32. thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`
  33. (left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5
  34. ```
  35. ### Fixing the Error
  36. To fix it, open up `src/lib.rs` and change the `hello` function to return
  37. `"Hello, World!"` instead of `"Goodbye, World!"`.
  38. ```rust
  39. pub fn hello() -> &'static str {
  40. "Hello, World!"
  41. }
  42. ```
  43. ## Step 2
  44. Run the test again. This time, it will pass.
  45. ```
  46. running 0 tests
  47. test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  48. Running target/debug/deps/hello_world-bd1f06dc726ef14f
  49. running 1 test
  50. test test_hello_world ... ok
  51. test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
  52. Doc-tests hello-world
  53. running 0 tests
  54. test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
  55. ```
  56. ## Submit
  57. Once the test is passing, you can submit your code with the following
  58. command:
  59. ```
  60. $ exercism submit src/lib.rs
  61. ```