Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update hello-world GETTING_STARTED.md to match simplified exercise #266

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 29 additions & 97 deletions exercises/hello-world/GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,141 +18,73 @@ Run the test suite. It can be run with `cargo`, which is installed with rust.
$ cargo test
```

This will fail, complaining that `hello-world` could not compile.
This will compile the `hello-world` crate and run the test, which fails.

To fix this, create a new directory called `src`.
Create a new file called, `lib.rs`, inside the `src` directory.

## Step 2

Run the test again. It will give you a new error, another compile error.
Our `lib.rs` does not contain any code, specifically the `hello()`
function that our test is looking for.

### Fixing the Error

To fix it, open up the `src/lib.rs` file and add the following code:

```rust
pub fn hello(name: Option<&str>) -> String {
"".to_string()
}
```

Our test is looking for the `hello()` function from the `hello_world`
crate. `lib.rs`, by default, is our crate root and our test
is looking for the `hello()` function there.

The code we are adding to `lib.rs` defines a public function (`pub fn`) that is called "hello".
The function accepts a `name` as an optional argument (`Option`).
The function returns a `String`.
We start by returning an empty string (`"".to_string()`).

## Step 3

Run the test again.

This time, code compilation will pass and we receive actual test failures.

```
running 3 tests
test test_other_same_name ... ignored
test test_sample_name ... ignored
test test_no_name ... FAILED
running 1 test
test test_hello_world ... FAILED

failures:

---- test_no_name stdout ----
thread 'test_no_name' panicked at 'assertion failed: `(left == right)`
(left: `"Hello, World!"`, right: `""`)', tests/hello-world.rs:5

---- 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_no_name
test_hello_world

test result: FAILED. 0 passed; 1 failed; 2 ignored; 0 measured
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
```

### Understanding Test Failures

Only one of the tests runs (`test_no_name`) and it fails. The other
tests are ignored (more on that later).

The `test_no_name` failure states that it is expecting the value,
`"Hello, World!"`, to be returned from `hello("")`.
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.

```
---- test_no_name stdout ----
thread 'test_no_name' panicked at 'assertion failed: `(left == right)`
(left: `"Hello, World!"`, right: `""`)', tests/hello-world.rs:5
---- 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
```

To fix it, let's return `"Hello, World!"`, instead of an empty string
(`""`) inside our `hello` function.
### 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(name: Option<&str>) -> String {
"Hello, World!".to_string()
pub fn hello() -> &'static str {
"Hello, World!"
}
```

## Step 4
## Step 2

Run the test again. This time, it will pass.

```
running 3 tests
test test_other_same_name ... ignored
test test_sample_name ... ignored
test test_no_name ... ok

test result: ok. 1 passed; 0 failed; 2 ignored; 0 measured

Doc-tests hello-world

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
```

You may have noticed compiler warnings earlier:

```
Compiling hello-world v0.0.0
(file:////exercism/exercises/rust/hello-world)
src/lib.rs:1:14: 1:18 warning: unused variable: `name`, #[warn(unused_variables)] on by default
src/lib.rs:1 pub fn hello(name: Option<&str>) -> String {
^~~~
```

Our `hello` function does not use the `name` argument so the
compiler is letting us know that we could potentially remove the
argument from our function (it likes "clean code").

As we make the rest of the tests pass, we will find that we need the `name`
argument, so don't delete it.

Activate the next test. Open the `tests/hello-world.rs` file.
Delete the `#[ignore]` line for the `test_sample_name` test.
Running target/debug/deps/hello_world-bd1f06dc726ef14f

## Step 5
running 1 test
test test_hello_world ... ok

Run the test suite again. Read the test failure and make the test pass.
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

As a reminder, the [rust book](http://doc.rust-lang.org/stable/book/README.html)
is a good reference for understanding rust.
The cargo output may also have hints to help you, depending on the errors you get.
For example, `rustc --explain E0425` will explain unresolved name errors.
Doc-tests hello-world

## Wash, Rinse, Repeat
running 0 tests

Delete one `#[ignore]` at a time, and make each test pass before you move to
the next one.
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
```

## Submit

When everything is passing, you can submit your code with the following
Once the test is passing, you can submit your code with the following
command:

```
Expand Down