Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8670b0

Browse files
🚀begin hello-world
1 parent 76d636f commit e8670b0

File tree

6 files changed

+210
-0
lines changed

6 files changed

+210
-0
lines changed

‎hello-world/.gitignore‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8+
Cargo.lock

‎hello-world/Cargo.toml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
edition = "2018"
3+
name = "hello-world"
4+
version = "1.1.0"

‎hello-world/GETTING_STARTED.md‎

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

‎hello-world/README.md‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Hello World
2+
3+
The classical introductory exercise. Just say "Hello, World!".
4+
5+
["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
6+
the traditional first program for beginning programming in a new language
7+
or environment.
8+
9+
The 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+
15+
If everything goes well, you will be ready to fetch your first real exercise.
16+
17+
## Rust Installation
18+
19+
Refer to the [exercism help page][help-page] for Rust installation and learning
20+
resources.
21+
22+
## Writing the Code
23+
24+
Execute the tests with:
25+
26+
```bash
27+
$ cargo test
28+
```
29+
30+
All but the first test have been ignored. After you get the first test to
31+
pass, open the tests source file which is located in the `tests` directory
32+
and remove the `#[ignore]` flag from the next test and get the tests to pass
33+
again. Each separate test is a function with `#[test]` flag above it.
34+
Continue, until you pass every test.
35+
36+
If you wish to run all tests without editing the tests source file, use:
37+
38+
```bash
39+
$ cargo test -- --ignored
40+
```
41+
42+
To run a specific test, for example `some_test`, you can use:
43+
44+
```bash
45+
$ cargo test some_test
46+
```
47+
48+
If the specific test is ignored use:
49+
50+
```bash
51+
$ cargo test some_test -- --ignored
52+
```
53+
54+
To learn more about Rust tests refer to the [online test documentation][rust-tests]
55+
56+
Make sure to read the [Modules][modules] chapter if you
57+
haven't already, it will help you with organizing your files.
58+
59+
## Further improvements
60+
61+
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.
62+
63+
To format your solution, inside the solution directory use
64+
65+
```bash
66+
cargo fmt
67+
```
68+
69+
To see, if your solution contains some common ineffective use cases, inside the solution directory use
70+
71+
```bash
72+
cargo clippy --all-targets
73+
```
74+
75+
## Submitting the solution
76+
77+
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.
78+
79+
## Feedback, Issues, Pull Requests
80+
81+
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!
82+
83+
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).
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+
92+
This 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
95+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

‎hello-world/src/lib.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3+
pub fn hello() -> &'static str {
4+
"Goodbye, World!"
5+
}

‎hello-world/tests/hello-world.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use hello_world;
2+
3+
#[test]
4+
fn test_hello_world() {
5+
assert_eq!("Hello, World!", hello_world::hello());
6+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /