rust - hello-world
This commit is contained in:
parent
64d4b70158
commit
3a6122e886
8
rust/hello-world/.gitignore
vendored
Normal file
8
rust/hello-world/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# 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-cargolock
|
||||
Cargo.lock
|
3
rust/hello-world/Cargo.toml
Normal file
3
rust/hello-world/Cargo.toml
Normal file
@ -0,0 +1,3 @@
|
||||
[package]
|
||||
name = "hello-world"
|
||||
version = "1.1.0"
|
92
rust/hello-world/GETTING_STARTED.md
Normal file
92
rust/hello-world/GETTING_STARTED.md
Normal file
@ -0,0 +1,92 @@
|
||||
# Getting Started
|
||||
|
||||
These exercises lean on Test-Driven Development (TDD), but they're not
|
||||
an 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/2018-edition/ch01-01-installation.html).
|
||||
The [Rust language section](http://exercism.io/languages/rust)
|
||||
section from exercism is also useful.
|
||||
|
||||
## Step 1
|
||||
|
||||
Run 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 test
|
||||
test test_hello_world ... FAILED
|
||||
|
||||
failures:
|
||||
|
||||
---- 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_hello_world
|
||||
|
||||
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
|
||||
```
|
||||
|
||||
### Understanding Test Failures
|
||||
|
||||
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_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 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() -> &'static str {
|
||||
"Hello, World!"
|
||||
}
|
||||
```
|
||||
|
||||
## Step 2
|
||||
|
||||
Run the test again. This time, it will pass.
|
||||
|
||||
```
|
||||
running 0 tests
|
||||
|
||||
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
|
||||
|
||||
Running target/debug/deps/hello_world-bd1f06dc726ef14f
|
||||
|
||||
running 1 test
|
||||
test test_hello_world ... ok
|
||||
|
||||
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
|
||||
|
||||
Doc-tests hello-world
|
||||
|
||||
running 0 tests
|
||||
|
||||
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
|
||||
```
|
||||
|
||||
## Submit
|
||||
|
||||
Once the test is passing, you can submit your code with the following
|
||||
command:
|
||||
|
||||
```
|
||||
$ exercism submit src/lib.rs
|
||||
```
|
75
rust/hello-world/README.md
Normal file
75
rust/hello-world/README.md
Normal file
@ -0,0 +1,75 @@
|
||||
# Hello World
|
||||
|
||||
The classical introductory exercise. Just say "Hello, World!".
|
||||
|
||||
["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
|
||||
the traditional first program for beginning programming in a new language
|
||||
or 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 Installation
|
||||
|
||||
Refer to the [exercism help page][help-page] for Rust installation and learning
|
||||
resources.
|
||||
|
||||
## Writing the Code
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ cargo test
|
||||
```
|
||||
|
||||
All but the first test have been ignored. After you get the first test to
|
||||
pass, open the tests source file which is located in the `tests` directory
|
||||
and remove the `#[ignore]` flag from the next test and get the tests to pass
|
||||
again. Each separate test is a function with `#[test]` flag above it.
|
||||
Continue, until you pass every test.
|
||||
|
||||
If you wish to run all 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](https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html) chapter if you
|
||||
haven't already, it will help you with organizing your files.
|
||||
|
||||
## Feedback, Issues, Pull Requests
|
||||
|
||||
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!
|
||||
|
||||
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).
|
||||
|
||||
[help-page]: https://exercism.io/tracks/rust/learning
|
||||
[modules]: https://doc.rust-lang.org/book/2018-edition/ch07-00-modules.html
|
||||
[cargo]: https://doc.rust-lang.org/book/2018-edition/ch14-00-more-about-cargo.html
|
||||
[rust-tests]: https://doc.rust-lang.org/book/2018-edition/ch11-02-running-tests.html
|
||||
|
||||
## Source
|
||||
|
||||
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)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
5
rust/hello-world/src/lib.rs
Normal file
5
rust/hello-world/src/lib.rs
Normal file
@ -0,0 +1,5 @@
|
||||
// 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 {
|
||||
"Hello, World!"
|
||||
}
|
6
rust/hello-world/tests/hello-world.rs
Normal file
6
rust/hello-world/tests/hello-world.rs
Normal file
@ -0,0 +1,6 @@
|
||||
extern crate hello_world;
|
||||
|
||||
#[test]
|
||||
fn test_hello_world() {
|
||||
assert_eq!("Hello, World!", hello_world::hello());
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user