aboutsummaryrefslogtreecommitdiff
path: root/rust/hello-world
diff options
context:
space:
mode:
Diffstat (limited to 'rust/hello-world')
-rw-r--r--rust/hello-world/.exercism/metadata.json1
-rw-r--r--rust/hello-world/.gitignore8
-rw-r--r--rust/hello-world/Cargo.toml4
-rw-r--r--rust/hello-world/GETTING_STARTED.md92
-rw-r--r--rust/hello-world/README.md95
-rw-r--r--rust/hello-world/src/lib.rs5
-rw-r--r--rust/hello-world/tests/hello-world.rs6
7 files changed, 211 insertions, 0 deletions
diff --git a/rust/hello-world/.exercism/metadata.json b/rust/hello-world/.exercism/metadata.json
new file mode 100644
index 0000000..0e3e354
--- /dev/null
+++ b/rust/hello-world/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"hello-world","id":"aac7852c428e494ca701dc74d200c468","url":"https://exercism.io/my/solutions/aac7852c428e494ca701dc74d200c468","handle":"dyamon","is_requester":true,"auto_approve":true} \ No newline at end of file
diff --git a/rust/hello-world/.gitignore b/rust/hello-world/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/rust/hello-world/.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/hello-world/Cargo.toml b/rust/hello-world/Cargo.toml
new file mode 100644
index 0000000..4d9f8bb
--- /dev/null
+++ b/rust/hello-world/Cargo.toml
@@ -0,0 +1,4 @@
1[package]
2edition = "2018"
3name = "hello-world"
4version = "1.1.0"
diff --git a/rust/hello-world/GETTING_STARTED.md b/rust/hello-world/GETTING_STARTED.md
new file mode 100644
index 0000000..5eeea95
--- /dev/null
+++ b/rust/hello-world/GETTING_STARTED.md
@@ -0,0 +1,92 @@
1# Getting Started
2
3These exercises lean on Test-Driven Development (TDD), but they're not
4an exact match.
5
6The following steps assume that you are in the same directory as the exercise.
7
8You must have rust installed.
9Follow the [Installation chapter in the Rust book](https://doc.rust-lang.org/book/ch01-01-installation.html).
10The [Rust language section](http://exercism.io/languages/rust)
11section from exercism is also useful.
12
13## Step 1
14
15Run the test suite. It can be run with `cargo`, which is installed with rust.
16
17```
18$ cargo test
19```
20
21This will compile the `hello-world` crate and run the test, which fails.
22
23```
24running 1 test
25test test_hello_world ... FAILED
26
27failures:
28
29---- test_hello_world stdout ----
30thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`
31(left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5
32
33failures:
34 test_hello_world
35
36test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
37```
38
39### Understanding Test Failures
40
41The `test_hello_world` failure states that it is expecting the value,
42`"Hello, World!"`, to be returned from `hello()`.
43The left side of the assertion (at line 5) should be equal to the right side.
44
45```
46---- test_hello_world stdout ----
47thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`
48(left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5
49```
50
51### Fixing the Error
52
53To fix it, open up `src/lib.rs` and change the `hello` function to return
54`"Hello, World!"` instead of `"Goodbye, World!"`.
55
56```rust
57pub fn hello() -> &'static str {
58 "Hello, World!"
59}
60```
61
62## Step 2
63
64Run the test again. This time, it will pass.
65
66```
67running 0 tests
68
69test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
70
71 Running target/debug/deps/hello_world-bd1f06dc726ef14f
72
73running 1 test
74test test_hello_world ... ok
75
76test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
77
78 Doc-tests hello-world
79
80running 0 tests
81
82test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
83```
84
85## Submit
86
87Once the test is passing, you can submit your code with the following
88command:
89
90```
91$ exercism submit src/lib.rs
92```
diff --git a/rust/hello-world/README.md b/rust/hello-world/README.md
new file mode 100644
index 0000000..775dd89
--- /dev/null
+++ b/rust/hello-world/README.md
@@ -0,0 +1,95 @@
1# Hello World
2
3The classical introductory exercise. Just say "Hello, World!".
4
5["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
6the traditional first program for beginning programming in a new language
7or environment.
8
9The objectives are simple:
10
11- Write a function that returns the string "Hello, World!".
12- Run the test suite and make sure that it succeeds.
13- Submit your solution and check it at the website.
14
15If everything goes well, you will be ready to fetch your first real exercise.
16
17## Rust Installation
18
19Refer to the [exercism help page][help-page] for Rust installation and learning
20resources.
21
22## Writing the Code
23
24Execute the tests with:
25
26```bash
27$ cargo test
28```
29
30All but the first test have been ignored. After you get the first test to
31pass, open the tests source file which is located in the `tests` directory
32and remove the `#[ignore]` flag from the next test and get the tests to pass
33again. Each separate test is a function with `#[test]` flag above it.
34Continue, until you pass every test.
35
36If you wish to run all ignored tests without editing the tests source file, use:
37
38```bash
39$ cargo test -- --ignored
40```
41
42To run a specific test, for example `some_test`, you can use:
43
44```bash
45$ cargo test some_test
46```
47
48If the specific test is ignored use:
49
50```bash
51$ cargo test some_test -- --ignored
52```
53
54To learn more about Rust tests refer to the [online test documentation][rust-tests]
55
56Make sure to read the [Modules][modules] chapter if you
57haven't already, it will help you with organizing your files.
58
59## Further improvements
60
61After 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.
62
63To format your solution, inside the solution directory use
64
65```bash
66cargo fmt
67```
68
69To see, if your solution contains some common ineffective use cases, inside the solution directory use
70
71```bash
72cargo clippy --all-targets
73```
74
75## Submitting the solution
76
77Generally 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.
78
79## Feedback, Issues, Pull Requests
80
81The [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!
82
83If 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).
84
85[help-page]: https://exercism.io/tracks/rust/learning
86[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
87[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
88[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
89
90## Source
91
92This 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)
93
94## Submitting Incomplete Solutions
95It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/rust/hello-world/src/lib.rs b/rust/hello-world/src/lib.rs
new file mode 100644
index 0000000..8f68273
--- /dev/null
+++ b/rust/hello-world/src/lib.rs
@@ -0,0 +1,5 @@
1// The &'static here means the return type has a static lifetime.
2// This is a Rust feature that you don't need to worry about now.
3pub fn hello() -> &'static str {
4 "Hello, World!"
5}
diff --git a/rust/hello-world/tests/hello-world.rs b/rust/hello-world/tests/hello-world.rs
new file mode 100644
index 0000000..c0d9536
--- /dev/null
+++ b/rust/hello-world/tests/hello-world.rs
@@ -0,0 +1,6 @@
1use hello_world;
2
3#[test]
4fn test_hello_world() {
5 assert_eq!("Hello, World!", hello_world::hello());
6}