Initial Commit
This commit is contained in:
1
rust/current
Symbolic link
1
rust/current
Symbolic link
@@ -0,0 +1 @@
|
||||
hello-world
|
4
rust/hello-world/Cargo.lock
generated
Normal file
4
rust/hello-world/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
[root]
|
||||
name = "hello-world"
|
||||
version = "0.0.0"
|
||||
|
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 = "0.0.0"
|
160
rust/hello-world/GETTING_STARTED.md
Normal file
160
rust/hello-world/GETTING_STARTED.md
Normal file
@@ -0,0 +1,160 @@
|
||||
# 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 [Installing Rust chapter in the Rust book](http://doc.rust-lang.org/stable/book/installing-rust.html).
|
||||
The [getting started with rust](http://help.exercism.io/getting-started-with-rust.html)
|
||||
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 fail, complaining that `hello-world` could not compile.
|
||||
|
||||
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
|
||||
|
||||
failures:
|
||||
|
||||
---- test_no_name stdout ----
|
||||
thread 'test_no_name' panicked at 'assertion failed: `(left == right)`
|
||||
(left: `"Hello, World!"`, right: `""`)', tests/hello-world.rs:5
|
||||
|
||||
|
||||
failures:
|
||||
test_no_name
|
||||
|
||||
test result: FAILED. 0 passed; 1 failed; 2 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 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
|
||||
```
|
||||
|
||||
To fix it, let's return `"Hello, World!"`, instead of an empty string
|
||||
(`""`) inside our `hello` function.
|
||||
|
||||
```rust
|
||||
pub fn hello(name: Option<&str>) -> String {
|
||||
"Hello, World!".to_string()
|
||||
}
|
||||
```
|
||||
|
||||
## Step 4
|
||||
|
||||
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.
|
||||
|
||||
## Step 5
|
||||
|
||||
Run the test suite again. Read the test failure and make the test pass.
|
||||
|
||||
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.
|
||||
|
||||
## Wash, Rinse, Repeat
|
||||
|
||||
Delete one `#[ignore]` at a time, and make each test pass before you move to
|
||||
the next one.
|
||||
|
||||
## Submit
|
||||
|
||||
When everything is passing, you can submit your code with the following
|
||||
command:
|
||||
|
||||
```
|
||||
$ exercism submit src/lib.rs
|
||||
```
|
68
rust/hello-world/README.md
Normal file
68
rust/hello-world/README.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# Hello World
|
||||
|
||||
Write a program that greets the user by name, or by saying "Hello, World!" if no name is given.
|
||||
|
||||
["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is the traditional first program for beginning programming in a new language.
|
||||
|
||||
**Note:** You can skip this exercise by running:
|
||||
|
||||
exercism skip $LANGUAGE hello-world
|
||||
|
||||
## Specification
|
||||
|
||||
The `Hello World!` program will greet me, the caller.
|
||||
|
||||
If I tell the program my name is Alice, it will greet me by saying "Hello, Alice!".
|
||||
|
||||
If I neglect to give it my name, it will greet me by saying "Hello, World!"
|
||||
|
||||
## Test-Driven Development
|
||||
|
||||
As programmers mature, they eventually want to test their code.
|
||||
|
||||
Here at Exercism we simulate [Test-Driven Development](http://en.wikipedia.org/wiki/Test-driven_development) (TDD), where you write your tests before writing any functionality. The simulation comes in the form of a pre-written test suite, which will signal that you have solved the problem.
|
||||
|
||||
It will also provide you with a safety net to explore other solutions without breaking the functionality.
|
||||
|
||||
### A typical TDD workflow on Exercism:
|
||||
|
||||
1. Run the test file and pick one test that's failing.
|
||||
2. Write some code to fix the test you picked.
|
||||
3. Re-run the tests to confirm the test is now passing.
|
||||
4. Repeat from step 1.
|
||||
5. [Submit your solution](http://help.exercism.io/submitting-exercises.html).
|
||||
|
||||
## Instructions
|
||||
|
||||
Submissions are encouraged to be general, within reason. Having said that, it's also important not to over-engineer a solution.
|
||||
|
||||
It's important to remember that the goal is to make code as expressive and readable as we can. However, solutions to the hello-world exercise will be not be reviewed by a person, but by rikki- the robot, who will offer an encouraging word.
|
||||
|
||||
## 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, remove the ignore flag (`#[ignore]`) from the next test and get the tests
|
||||
to pass again. The test file is located in the `tests` directory. You can
|
||||
also remove the ignore flag from all the tests to get them to run all at once
|
||||
if you wish.
|
||||
|
||||
Make sure to read the [Crates and Modules](crates-and-modules) chapter if you
|
||||
haven't already, it will help you with organizing your files.
|
||||
|
||||
[help-page]: http://help.exercism.io/getting-started-with-rust.html
|
||||
[crates-and-modules]: http://doc.rust-lang.org/stable/book/crates-and-modules.html
|
||||
|
||||
## Source
|
||||
|
||||
This is a program to introduce users to using Exercism [view source](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program)
|
18
rust/hello-world/tests/hello-world.rs
Normal file
18
rust/hello-world/tests/hello-world.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
extern crate hello_world;
|
||||
|
||||
#[test]
|
||||
fn test_no_name() {
|
||||
assert_eq!("Hello, World!", hello_world::hello(None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_sample_name() {
|
||||
assert_eq!("Hello, Alice!", hello_world::hello(Some("Alice")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_other_same_name() {
|
||||
assert_eq!("Hello, Bob!", hello_world::hello(Some("Bob")));
|
||||
}
|
4
rust/leap/Cargo.lock
generated
Normal file
4
rust/leap/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
[root]
|
||||
name = "leap"
|
||||
version = "0.0.0"
|
||||
|
3
rust/leap/Cargo.toml
Normal file
3
rust/leap/Cargo.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
[package]
|
||||
name = "leap"
|
||||
version = "0.0.0"
|
53
rust/leap/README.md
Normal file
53
rust/leap/README.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# Leap
|
||||
|
||||
Write a program that will take a year and report if it is a leap year.
|
||||
|
||||
The tricky thing here is that a leap year occurs:
|
||||
|
||||
```plain
|
||||
on every year that is evenly divisible by 4
|
||||
except every year that is evenly divisible by 100
|
||||
unless the year is also evenly divisible by 400
|
||||
```
|
||||
|
||||
For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap
|
||||
year, but 2000 is.
|
||||
|
||||
If your language provides a method in the standard library that does
|
||||
this look-up, pretend it doesn't exist and implement it yourself.
|
||||
|
||||
## Notes
|
||||
|
||||
For a delightful, four minute explanation of the whole leap year
|
||||
phenomenon, go watch [this youtube video][video].
|
||||
|
||||
[video]: http://www.youtube.com/watch?v=xX96xng7sAE
|
||||
|
||||
## 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, remove the ignore flag (`#[ignore]`) from the next test and get the tests
|
||||
to pass again. The test file is located in the `tests` directory. You can
|
||||
also remove the ignore flag from all the tests to get them to run all at once
|
||||
if you wish.
|
||||
|
||||
Make sure to read the [Crates and Modules](crates-and-modules) chapter if you
|
||||
haven't already, it will help you with organizing your files.
|
||||
|
||||
[help-page]: http://help.exercism.io/getting-started-with-rust.html
|
||||
[crates-and-modules]: http://doc.rust-lang.org/stable/book/crates-and-modules.html
|
||||
|
||||
## Source
|
||||
|
||||
JavaRanch Cattle Drive, exercise 3 [view source](http://www.javaranch.com/leap.jsp)
|
24
rust/leap/tests/leap.rs
Normal file
24
rust/leap/tests/leap.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
extern crate leap;
|
||||
|
||||
#[test]
|
||||
fn test_vanilla_leap_year() {
|
||||
assert_eq!(leap::is_leap_year(1996), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_any_old_year() {
|
||||
assert_eq!(leap::is_leap_year(1997), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_century() {
|
||||
assert_eq!(leap::is_leap_year(1900), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_exceptional_century() {
|
||||
assert_eq!(leap::is_leap_year(2000), true);
|
||||
}
|
Reference in New Issue
Block a user