···11+{
22+ "blurb": "Learn about numbers while working on an assembly line for cars.",
33+ "authors": [
44+ "LewisClement",
55+ "efx"
66+ ],
77+ "files": {
88+ "solution": [
99+ "src/lib.rs"
1010+ ],
1111+ "test": [
1212+ "tests/assembly-line.rs"
1313+ ],
1414+ "exemplar": [
1515+ ".meta/exemplar.rs"
1616+ ]
1717+ }
1818+}
+7
rust/assembly-line/Cargo.lock
···11+# This file is automatically @generated by Cargo.
22+# It is not intended for manual editing.
33+version = 3
44+55+[[package]]
66+name = "assembly-line"
77+version = "0.1.0"
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+13
rust/assembly-line/HINTS.md
···11+# Hints
22+33+## General
44+55+## 1. Calculate the production rate per hour
66+77+- Determining the success rate can be done through a [conditional statement](https://doc.rust-lang.org/stable/book/ch03-05-control-flow.html#if-expressions) or with [pattern matching](https://doc.rust-lang.org/stable/book/ch18-01-all-the-places-for-patterns.html#match-arms).
88+- As Rust only allows multiplication between values of the same type, some [type casting](https://doc.rust-lang.org/rust-by-example/types/cast.html) will have to be done.
99+1010+## 2. Calculate the number of working items produced per minute
1111+1212+1313+- Just like multiplication, division is only possible between numbers of the same type. By writing a number with a decimal point (e.g. `1.0` instead of `1`) we can write inline constants with a floating point type.
+86
rust/assembly-line/README.md
···11+# Assembly Line
22+33+Welcome to Assembly Line on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+## numbers
1010+1111+There are two different categories of numbers in Rust.
1212+1313+The name of a numeric type consists of two parts:
1414+1515+- A letter to specify whether it's a floating-point number (f), unsigned integer (u) or signed integer (i)
1616+- A number to specify the type's size in bits. Larger types have a greater range between minimum and maximum values.
1717+ For floating points it will also allow for more numbers behind the decimal separator.
1818+1919+The following combinations are possible:
2020+2121+- 8 bits: `u8`, `i8`
2222+- 16 bits: `u16`, `i16`
2323+- 32 bits: `u32`, `i32`, `f32`
2424+- 64 bits: `u64`, `i64`, `f64`
2525+- 128 bits: `u128`, `i128`
2626+2727+Note that there are only 32-bits and 64-bits variants for floating-point numbers.
2828+2929+## Integers
3030+3131+- Integers: numbers with no digits behind the decimal separator (whole numbers).
3232+ Integer types can either store only positive numbers (unsigned) or store either positive and negative numbers (signed).
3333+ Examples are -6, 0, 1, 25, 976 and 500000.
3434+3535+## Floating Point Numbers
3636+3737+- Floating-point numbers: numbers with zero or more digits behind the decimal separator.
3838+ Examples are -2.4, 0.1, 3.14, 16.984025 and 1024.0.
3939+4040+## Converting between number types
4141+4242+Rust doesn't do any implicit type conversion.
4343+This means that if you need to turn one numeric type into another, you have to do so explicitly.
4444+When converting from a larger type to a smaller one (for instance `u64` to `u32`) you could lose data.
4545+Converting from a floating point to an integer **will** lose everything behind the decimal point, effectively rounding down.
4646+4747+## Instructions
4848+4949+In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from `0` (off) to `10` (maximum).
5050+5151+At its lowest speed (`1`), `221` cars are produced each hour. The production increases linearly with the speed. So with the speed set to `4`, it should produce `4 * 221 = 884` cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. The following table shows how speed influences the success rate:
5252+5353+- `1` to `4`: 100% success rate.
5454+- `5` to `8`: 90% success rate.
5555+- `9` and `10`: 77% success rate.
5656+5757+You have two tasks.
5858+5959+## 1. Calculate the production rate per hour
6060+6161+Implement a method to calculate the assembly line's production rate per hour, taking into account its success rate:
6262+6363+```rust
6464+numbers::production_rate_per_hour(6)
6565+// Returns: 1193.4
6666+```
6767+6868+Note that the value returned is an `f64`.
6969+7070+## 2. Calculate the number of working items produced per minute
7171+7272+Implement a method to calculate how many working cars are produced per minute:
7373+7474+```rust
7575+numbers::working_items_per_minute(6)
7676+// Returns: 19
7777+```
7878+7979+Note that the value returned is an `u32`.
8080+8181+## Source
8282+8383+### Created by
8484+8585+- @LewisClement
8686+- @efx
···11+# This file is automatically @generated by Cargo.
22+# It is not intended for manual editing.
33+version = 3
44+55+[[package]]
66+name = "health_statistics"
77+version = "0.1.0"
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+31
rust/health-statistics/HINTS.md
···11+# Hints
22+33+## General
44+55+## 1. Implement the `new()` method
66+77+- The `new()` method receives the arguments we want to instantiate a `User` instance with. It should return an instance of `User` with the specified name, age, and weight.
88+99+- See [here](https://doc.rust-lang.org/book/ch05-01-defining-structs.html) for additional examples on defining and instantiating structs.
1010+1111+## 2. Implement the getter methods
1212+1313+- The `name()`, `age()`, and `weight()` methods are getters. In other words, they are responsible for returning the corresponding field from a struct instance.
1414+1515+- Notice that the `name` method returns a `&str` when the `name` field on the `User` struct is a `String`. How can we get `&str` and `String` to play nice with each other?
1616+1717+- There's no need to use a `return` statement in Rust unless you expressly want a function or method to return early. Otherwise, it's more idiomatic to utilize an _implicit_ return by omitting the semicolon for the result we want a function or method to return. It's not _wrong_ to use an explicit return, but it's cleaner to take advantage of implicit returns where possible.
1818+1919+```rust
2020+fn foo() -> i32 {
2121+ 1
2222+}
2323+```
2424+2525+- See [here](https://doc.rust-lang.org/book/ch05-03-method-syntax.html) for some more examples of defining methods on structs.
2626+2727+## 3. Implement the setter methods
2828+2929+- The `set_age()` and `set_weight()` methods are setters, responsible for updating the corresponding field on a struct instance with the input argument.
3030+3131+- As the signatures of these methods specify, the setter methods shouldn't return anything.
+91
rust/health-statistics/README.md
···11+# Health Statistics
22+33+Welcome to Health Statistics on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+It is often useful to group a collection of items together, and handle those groups as units. In Rust, we call such a group a struct, and each item one of the struct's fields. A struct defines the general set of fields available, but a particular example of a struct is called an instance.
1010+1111+Furthermore, structs can have methods defined on them, which have access to the fields. The struct itself in that case is referred to as `self`. When a method uses `&mut self`, the fields can be changed, or mutated. When a method uses `&self`, the fields cannot be changed: they are immutable. Controlling mutability helps the borrow-checker ensure that entire classes of concurrency bug just don't happen in Rust.
1212+1313+In this exercise, you'll be implementing two kinds of methods on a struct. The first are generally known as getters: they expose the struct's fields to the world, without letting anyone else mutate that value. In Rust, these methods idiomatically share the name of the field they expose, i.e., if we have a getter method that fetches a struct field called `name`, the method would simply be called `name()`.
1414+1515+You'll also be implementing methods of another type, generally known as setters. These change the value of the field. Setters aren't very common in Rust--if a field can be freely modified, it is more common to just make it public--but they're useful if updating the field should have side effects, or for access control: a setter marked as `pub(crate)` allows other modules within the same crate to update a private field, which can't be affected by the outside world.
1616+1717+Structs come in three flavors: structs with named fields, tuple structs, and unit structs. For this concept exercise, we'll be exploring the first variant: structs with named fields.
1818+1919+Structs are defined using the `struct` keyword, followed by the capitalized name of the type the struct is describing:
2020+2121+```rust
2222+struct Item {}
2323+```
2424+2525+Additional types are then brought into the struct body as _fields_ of the struct, each with their own type:
2626+2727+```rust
2828+struct Item {
2929+ name: String,
3030+ weight: f32,
3131+ worth: u32,
3232+}
3333+```
3434+3535+Lastly, methods can be defined on structs inside of an `impl` block:
3636+3737+```rust
3838+impl Item {
3939+ // initializes and returns a new instance of our Item struct
4040+ fn new() -> Self {
4141+ unimplemented!()
4242+ }
4343+}
4444+```
4545+4646+With that brief introduction to the syntax of structs out of the way, go ahead and take a look at the [instructions](instructions.md) for this exercise!
4747+4848+## Instructions
4949+5050+You're working on implementing a health-monitoring system. As part of that, you need to keep track of users' health statistics.
5151+5252+You'll start with some stubbed functions in an `impl` block as well as the following struct definition:
5353+5454+```rust
5555+pub struct User {
5656+ name: String,
5757+ age: u32,
5858+ weight: f32,
5959+}
6060+```
6161+6262+Your goal is to implement the stubbed out methods on the `User` `struct` defined in the `impl` block.
6363+6464+For example, the `new` method should return an instance of the `User` struct with the specified name, age, and weight values.
6565+6666+```rust
6767+let mut bob = User::new(String::from("Bob"), 32, 155.2);
6868+// Returns: a User with name "Bob", age 32, and weight 155.2
6969+```
7070+7171+The `weight` method should return the weight of the `User`.
7272+7373+```rust
7474+bob.weight();
7575+// Returns: 155.2
7676+```
7777+7878+The `set_age` method should set the age of the `User`.
7979+8080+```rust
8181+bob.set_age(33);
8282+// Updates Bob's age to 33; happy birthday Bob!
8383+```
8484+8585+Have fun!
8686+8787+## Source
8888+8989+### Created by
9090+9191+- @seanchen1991
···11+# Generated by Cargo
22+# will have compiled files and executables
33+/target/
44+**/*.rs.bk
55+66+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
77+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
88+Cargo.lock
···11+# Getting Started
22+33+These exercises lean on Test-Driven Development (TDD), but they're not
44+an exact match.
55+66+The following steps assume that you are in the same directory as the exercise.
77+88+You must have Rust installed.
99+Follow the [Installation chapter in the Rust book](https://doc.rust-lang.org/book/ch01-01-installation.html).
1010+The [Rust language section](http://exercism.io/languages/rust)
1111+section from exercism is also useful.
1212+1313+## Step 1
1414+1515+Run the test suite. It can be run with `cargo`, which is installed with Rust.
1616+1717+```sh
1818+$ cargo test
1919+```
2020+2121+This will compile the `hello-world` crate and run the test, which fails.
2222+2323+```sh
2424+running 1 test
2525+test test_hello_world ... FAILED
2626+2727+failures:
2828+2929+---- test_hello_world stdout ----
3030+thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`
3131+(left: `"Hello, World!"`, right: `"Goodbye, Mars!"`)', tests/hello-world.rs:5
3232+3333+failures:
3434+ test_hello_world
3535+3636+test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
3737+```
3838+3939+### Understanding Test Failures
4040+4141+The `test_hello_world` failure states that it is expecting the value,
4242+`"Hello, World!"`, to be returned from `hello()`.
4343+The left side of the assertion (at line 5) should be equal to the right side.
4444+4545+```sh
4646+---- test_hello_world stdout ----
4747+thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`
4848+(left: `"Hello, World!"`, right: `"Goodbye, Mars!"`)', tests/hello-world.rs:5
4949+```
5050+5151+### Fixing the Error
5252+5353+To fix it, open up `src/lib.rs` and change the `hello` function to return
5454+`"Hello, World!"` instead of `"Goodbye, Mars!"`.
5555+5656+```rust
5757+pub fn hello() -> &'static str {
5858+ "Hello, World!"
5959+}
6060+```
6161+6262+## Step 2
6363+6464+Run the test again. This time, it will pass.
6565+6666+```sh
6767+running 0 tests
6868+6969+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
7070+7171+ Running target/debug/deps/hello_world-bd1f06dc726ef14f
7272+7373+running 1 test
7474+test test_hello_world ... ok
7575+7676+test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
7777+7878+ Doc-tests hello-world
7979+8080+running 0 tests
8181+8282+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
8383+```
8484+8585+## Submit
8686+8787+Once the test is passing, you can submit your code with the following
8888+command:
8989+9090+```sh
9191+$ exercism submit src/lib.rs
9292+```
+85
rust/hello-world/HELP.md
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+53
rust/hello-world/README.md
···11+# Hello World
22+33+Welcome to Hello World on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+66+## Instructions
77+88+The classical introductory exercise. Just say "Hello, World!".
99+1010+["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
1111+the traditional first program for beginning programming in a new language
1212+or environment.
1313+1414+The objectives are simple:
1515+1616+- Write a function that returns the string "Hello, World!".
1717+- Run the test suite and make sure that it succeeds.
1818+- Submit your solution and check it at the website.
1919+2020+If everything goes well, you will be ready to fetch your first real exercise.
2121+2222+## Source
2323+2424+### Created by
2525+2626+- @EduardoBautista
2727+2828+### Contributed to by
2929+3030+- @ashleygwilliams
3131+- @ClashTheBunny
3232+- @coriolinus
3333+- @cwhakes
3434+- @dvoytik
3535+- @EduardoBautista
3636+- @efx
3737+- @ErikSchierboom
3838+- @hydhknn
3939+- @IanWhitney
4040+- @ijanos
4141+- @kytrinyx
4242+- @lutostag
4343+- @nfiles
4444+- @petertseng
4545+- @regnerjr
4646+- @rofrol
4747+- @stringparser
4848+- @xakon
4949+- @ZapAnton
5050+5151+### Based on
5252+5353+This is an exercise to introduce users to using Exercism - http://en.wikipedia.org/wiki/%22Hello,_world!%22_program
+4
rust/hello-world/src/lib.rs
···11+// &'static is a "lifetime specifier", something you'll learn more about later
22+pub fn hello() -> &'static str {
33+ "Hello, World!"
44+}
···11+# This file is automatically @generated by Cargo.
22+# It is not intended for manual editing.
33+version = 3
44+55+[[package]]
66+name = "low_power_embedded_game"
77+version = "0.1.0"
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+23
rust/low-power-embedded-game/HINTS.md
···11+# Hints
22+33+## General
44+55+- [Rust Book: The Tuple Type](https://doc.rust-lang.org/book/ch03-02-data-types.html#the-tuple-type)
66+- [Rust By Example: Tuples](https://doc.rust-lang.org/stable/rust-by-example/primitives/tuples.html)
77+- [Rust By Example: Destructuring](https://doc.rust-lang.org/stable/rust-by-example/flow_control/match/destructuring.html)
88+99+## 1. Write a function `divmod` which returns both the quotient and remainder of a division
1010+1111+- Don't worry about optimizing for efficiency; the naive implementation is fine
1212+1313+## 2. Write an iterator adaptor `evens` which returns the even items from an arbitrary iterator
1414+1515+- Just chain together the suggested methods and everything will work out
1616+- A number `n` is even if `n % 2 == 0`
1717+- A closure is a function with abbreviated syntax: the argument name(s) go within a pair of `|` symbols, and the expression follows. Unlike normal functions, it is not always necessary to explicitly state the type of each argument, just the name. For example, here is how you can construct an iterator of odd squares: `(0..).map(|n| 2 * n + 1).map(|n| n * n)`.
1818+1919+## 3. Implement a `manhattan` method on a `Position` tuple struct
2020+2121+- Don't worry about method syntax; just replacing the `unimplemented` portion within the `impl Position` block will do the right thing.
2222+- Consider that some values within a `Position` may be negative, but a distance is never negative.
2323+- Calculating the absolute value is [built-in](https://doc.rust-lang.org/std/primitive.i16.html#method.abs)
+162
rust/low-power-embedded-game/README.md
···11+# Low-Power Embedded Game
22+33+Welcome to Low-Power Embedded Game on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+Tuples are a lightweight way to group a fixed set of arbitrary types of data together. A tuple doesn't have
1010+a particular name; naming a data structure turns it into a `struct`. A tuple's fields don't have names;
1111+they are accessed by means of destructuring or by position.
1212+1313+## Syntax
1414+1515+### Creation
1616+1717+Tuples are always created with a tuple expression:
1818+1919+```rust
2020+// pointless but legal
2121+let unit = ();
2222+// single element
2323+let single_element = ("note the comma",);
2424+// two element
2525+let two_element = (123, "elements can be of differing types");
2626+```
2727+2828+Tuples can have an arbitrary number of elements.
2929+3030+### Access by destructuring
3131+3232+It is possible to access the elements of a tuple by destructuring. This just means assigning variable
3333+names to the individual elements of the tuple, consuming it.
3434+3535+```rust
3636+let (elem1, _elem2) = two_element;
3737+assert_eq!(elem1, 123);
3838+```
3939+4040+### Access by position
4141+4242+It is also possible to access the elements of a tuple by numeric positional index. Indexing, as always,
4343+begins at 0.
4444+4545+```rust
4646+let notation = single_element.0;
4747+assert_eq!(notation, "note the comma");
4848+```
4949+5050+## Tuple Structs
5151+5252+You will also be asked to work with tuple structs. Like normal structs, these are named types; unlike
5353+normal structs, they have anonymous fields. Their syntax is very similar to normal tuple syntax. It is
5454+legal to use both destructuring and positional access.
5555+5656+```rust
5757+struct TupleStruct(u8, i32);
5858+let my_tuple_struct = TupleStruct(123, -321);
5959+let neg = my_tuple_struct.1;
6060+let TupleStruct(byte, _) = my_tuple_struct;
6161+assert_eq!(neg, -321);
6262+assert_eq!(byte, 123);
6363+```
6464+6565+### Field Visibility
6666+6767+All fields of anonymous tuples are always public. However, fields of tuple structs have individual
6868+visibility which defaults to private, just like fields of standard structs. You can make the fields
6969+public with the `pub` modifier, just as in a standard struct.
7070+7171+```rust
7272+// fails due to private fields
7373+mod tuple { pub struct TupleStruct(u8, i32); }
7474+fn main() { let _my_tuple_struct = tuple::TupleStruct(123, -321); }
7575+```
7676+7777+```rust
7878+// succeeds: fields are public
7979+mod tuple { pub struct TupleStruct(pub u8, pub i32); }
8080+fn main() { let _my_tuple_struct = tuple::TupleStruct(123, -321); }
8181+```
8282+8383+## Instructions
8484+8585+You are working on a game targeting a low-power embedded system and need to write several convenience functions which will be used by other parts of the game.
8686+8787+## 1. Calculate the quotient and remainder of a division
8888+8989+A quotient is the output of a division.
9090+9191+```rust
9292+fn divmod(dividend: i16, divisor: i16) -> (i16, i16)
9393+```
9494+9595+Example:
9696+9797+```rust
9898+assert_eq!(divmod(10, 3), (3, 1));
9999+```
100100+101101+## 2. Choose even-positioned items from an iterator
102102+103103+This will be helpful to enable a screen-buffer optimization, your boss promises.
104104+105105+Iterators are items which expose the methods defined by the [`Iterator` trait](https://doc.rust-lang.org/std/iter/trait.Iterator.html). That documentation is fairly extensive, because they offer many methods; here are the most relevant properties:
106106+107107+- An iterator is an arbitrary-length stream of items
108108+- They have an [`enumerate` method](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.enumerate) which returns a tuple `(i, val)` for each value
109109+- They have a [`filter` method](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter) which uses a closure to determine whether to yield an element of the iterator
110110+- They have a [`map` method](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.map) which uses a closure to modify elements of the iterator
111111+112112+Because your function can run on any kind of iterator, it uses `impl` to signify that this is a trait instance instead of a simple item. Likewise, the `<Item=T>` syntax just means that it doesn't matter what kind of item the iterator produces; your function can produce the even elements of any iterator.
113113+114114+```rust
115115+fn evens<T>(iter: impl Iterator<Item=T>) -> impl Iterator<Item=T>
116116+```
117117+118118+Examples:
119119+120120+```rust
121121+let mut even_ints = evens(0_u8..);
122122+assert_eq!(even_ints.next(), Some(0));
123123+assert_eq!(even_ints.next(), Some(2));
124124+assert_eq!(even_ints.next(), Some(4));
125125+assert_eq!(even_ints.next(), Some(6));
126126+```
127127+128128+```rust
129129+let mut evens_from_odds = evens(1_i16..);
130130+assert_eq!(evens_from_odds.next(), Some(1));
131131+assert_eq!(evens_from_odds.next(), Some(3));
132132+assert_eq!(evens_from_odds.next(), Some(5));
133133+assert_eq!(evens_from_odds.next(), Some(7));
134134+```
135135+136136+## 3. Calculate the manhattan distance of a position from the origin
137137+138138+For mapping convenience, you have a tuple struct `Position`:
139139+140140+```rust
141141+struct Position(i16, i16);
142142+```
143143+144144+You need to implement a method `manhattan` on `Position` which returns the [manhattan distance](https://en.wikipedia.org/wiki/Taxicab_geometry) of that position from the origin (`Position(0, 0)`).
145145+146146+```rust
147147+impl Position {
148148+ fn manhattan(&self) -> i16
149149+}
150150+```
151151+152152+Example:
153153+154154+```rust
155155+assert_eq!(Position(3, 4).manhattan(), 7);
156156+```
157157+158158+## Source
159159+160160+### Created by
161161+162162+- @coriolinus
···11+{
22+ "blurb": "Learn about the basics of Rust by following a lasagna recipe.",
33+ "icon": "lasagna",
44+ "authors": [
55+ "coriolinus",
66+ "ErikSchierboom"
77+ ],
88+ "files": {
99+ "solution": [
1010+ "src/lib.rs"
1111+ ],
1212+ "test": [
1313+ "tests/lucians-luscious-lasagna.rs"
1414+ ],
1515+ "exemplar": [
1616+ ".meta/exemplar.rs"
1717+ ]
1818+ }
1919+}
+7
rust/lucians-luscious-lasagna/Cargo.lock
···11+# This file is automatically @generated by Cargo.
22+# It is not intended for manual editing.
33+version = 3
44+55+[[package]]
66+name = "lucians-luscious-lasagna"
77+version = "0.1.0"
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+31
rust/lucians-luscious-lasagna/HINTS.md
···11+# Hints
22+33+## General
44+55+- An integer literal can be defined as one or more consecutive digits.
66+77+## 1. Define the expected oven time in minutes
88+99+- You need to define a [function][functions] without any parameters.
1010+1111+## 2. Calculate the remaining oven time in minutes
1212+1313+- You need to define a [function][functions] with a single parameter.
1414+- You can use and refer to the previously defined item by its name.
1515+- The last expression in a function is [automatically returned][return-values] from the function; you don't have to explicitly indicate which value to return.
1616+- You can use the [mathematical operator for subtraction][operators] to subtract values.
1717+1818+## 3. Calculate the preparation time in minutes
1919+2020+- You need to define a [function][functions] with a single parameter.
2121+- You can use the [mathematical operator for multiplicaton][operators] to multiply values.
2222+2323+## 4. Calculate the elapsed time in minutes
2424+2525+- You need to define a [function][functions] with two parameters.
2626+- You can [call][functions] one of the other functions you've defined previously.
2727+- You can use the [mathematical operator for addition][operators] to add values.
2828+2929+[functions]: https://doc.rust-lang.org/book/ch03-03-how-functions-work.html
3030+[return-values]: https://doc.rust-lang.org/book/ch03-03-how-functions-work.html#functions-with-return-values
3131+[operators]: https://doc.rust-lang.org/book/appendix-02-operators.html
+159
rust/lucians-luscious-lasagna/README.md
···11+# Lucian's Luscious Lasagna
22+33+Welcome to Lucian's Luscious Lasagna on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+In Rust, assigning a value to a name is referred to as a _binding_. Bindings are immutable unless declared with the `mut` keyword. As Rust is a statically-typed language, each binding has a type known at compile-time.
1010+1111+Bindings are most commonly defined using the `let` keyword. Specifying a binding's type is optional for most bindings, as Rust's _type inference_ can usually infer the type based on their value. A binding looks like this:
1212+1313+```rust
1414+// Automatically inferred type
1515+let fingers = 10;
1616+```
1717+1818+Functions are _items_. Where bindings typically refer to a particular value, items refer to a unit of code organization, typically a function or a module, which is available throughout the lifetime of the program. A function automatically returns the result of its last expression. A function may have 0 or more parameters, which are bindings with a lifetime of the function call.
1919+2020+Type inference is theoretically possible for functions, but is disabled as an intentional language design choice. While this means that you need to spend a little more time when writing code to specify precisely what a function's input and output types are, you save the time when you're reading the code, because all the input and output types are explicitly defined.
2121+2222+```rust
2323+fn add(x: i32, y: i32) -> i32 {
2424+ x + y
2525+}
2626+```
2727+2828+Invoking a function is done by specifying its name followed by parentheses. If the function requires parameters, an argument must be specified for each within the parentheses.
2929+3030+```rust
3131+let five = add(2, 3);
3232+```
3333+3434+If a binding's type cannot be inferred, the compiler will report an error. To fix this, add an explicit type annotation to the binding.
3535+3636+```rust
3737+// Explicit type annotation
3838+let fingers: i32 = 10;
3939+```
4040+4141+Items in Rust can be used before or after they are defined, because they have a static lifetime. Bindings, on the other hand, can only be used _after_ they have been defined. Using a binding before it has been defined results in a compile error.
4242+4343+```rust
4444+fn main() {
4545+ // `fn add` hasn't yet been defined, but that's perfectly ok
4646+ dbg!(add(3, 4));
4747+}
4848+4949+fn add(x: i32, y: i32) -> i32 {
5050+ x + y
5151+}
5252+```
5353+5454+```rust
5555+// this won't compile; `a` is used before its binding is defined
5656+let b = a;
5757+let a = x + y
5858+```
5959+6060+Rust uses curly braces (`{}`) to define a scope. A binding defined within a scope can't escape from it.
6161+6262+```rust
6363+let a = 1;
6464+dbg!(a); // 1
6565+{
6666+ // Here, we re-bind `a` to a new value, which is still immutable.
6767+ // This technique is called _shadowing_. The new binding is constrained to
6868+ // this anonymous scope. Outside this scope, the previous binding still
6969+ // applies.
7070+ let a = 2;
7171+ let b = 3;
7272+ dbg!(a, b); // 2, 3
7373+}
7474+// can't use `b` anymore because it is out of scope
7575+// dbg!(b);
7676+7777+// The shadowed `a` in the inner scope above has fallen out of scope,
7878+// leaving us with our original binding.
7979+dbg!(a); // 1
8080+```
8181+8282+Rust items are often organized in modules. Each crate is implicitly a module, but it can define inner sub-modules of arbitrary depth. A module groups related functionality and is defined using the `mod` keyword.
8383+8484+```rust
8585+mod calc_i32 {
8686+ fn add(a: i32, b: i32) -> i32 { a + b }
8787+ fn sub(a: i32, b: i32) -> i32 { a - b }
8888+ fn mul(a: i32, b: i32) -> i32 { a * b }
8989+ fn div(a: i32, b: i32) -> i32 { a / b }
9090+}
9191+```
9292+9393+Rust supports two types of comments. The keyword `//` indicates a single-line comment; everything following the keyword until the end of the line is ignored. The keywords `/*` and `*/` indicate a multi-line comment; everything within those two keywords is ignored. It is idiomatic and good practice to prefer single-line comments.
9494+9595+Rust also supports doc-comments, which show up in the generated documentation produced by `cargo doc`. Outer doc comments are formed with the keyword `///`, which acts identically to the `//` keyword. They apply to the item which follows them, such as a function:
9696+9797+```rust
9898+/// The `add` function produces the sum of its arguments.
9999+fn add(x: i32, y: i32) -> i32 { x + y }
100100+```
101101+102102+Inner doc comments are formed with the keyword `//!`, which acts identically to the `//` keyword. They apply to the item enclosing them, such as a module:
103103+104104+```rust
105105+mod my_cool_module {
106106+ //! This module is the bee's knees.
107107+}
108108+```
109109+110110+Doc comments can be of arbitrary length and contain markdown, which is rendered into the generated documentation.
111111+112112+## Instructions
113113+114114+In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.
115115+116116+You have four tasks, all related to the time spent cooking the lasagna.
117117+118118+## 1. Define the expected oven time in minutes
119119+120120+Define the `expected_minutes_in_oven` binding to check how many minutes the lasagna should be in the oven. According to the cooking book, the expected oven time in minutes is 40:
121121+122122+```rust
123123+expected_minutes_in_oven()
124124+// Returns: 40
125125+```
126126+127127+## 2. Calculate the remaining oven time in minutes
128128+129129+Define the `remaining_minutes_in_oven` function that takes the actual minutes the lasagna has been in the oven as a parameter and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.
130130+131131+```rust
132132+remaining_minutes_in_oven(30)
133133+// Returns: 10
134134+```
135135+136136+## 3. Calculate the preparation time in minutes
137137+138138+Define the `preparation_time_in_minutes` function that takes the number of layers you added to the lasagna as a parameter and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.
139139+140140+```rust
141141+preparation_time_in_minutes(2)
142142+// Returns: 4
143143+```
144144+145145+## 4. Calculate the elapsed time in minutes
146146+147147+Define the `elapsed_time_in_minutes` function that takes two parameters: the first parameter is the number of layers you added to the lasagna, and the second parameter is the number of minutes the lasagna has been in the oven. The function should return how many minutes you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.
148148+149149+```rust
150150+elapsed_time_in_minutes(3, 20)
151151+// Returns: 26
152152+```
153153+154154+## Source
155155+156156+### Created by
157157+158158+- @coriolinus
159159+- @ErikSchierboom
···11+{
22+ "blurb": "Use `HashMap` and the entry API methods to write an anonymous letter.",
33+ "authors": [
44+ "seanchen1991"
55+ ],
66+ "files": {
77+ "solution": [
88+ "src/lib.rs"
99+ ],
1010+ "test": [
1111+ "tests/magazine-cutout.rs"
1212+ ],
1313+ "exemplar": [
1414+ ".meta/exemplar.rs"
1515+ ]
1616+ }
1717+}
+7
rust/magazine-cutout/Cargo.lock
···11+# This file is automatically @generated by Cargo.
22+# It is not intended for manual editing.
33+version = 3
44+55+[[package]]
66+name = "magazine_cutout"
77+version = "0.1.0"
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+17
rust/magazine-cutout/HINTS.md
···11+# Hints
22+33+## General
44+55+- Upon fetching an entry using the `entry` method, the entry can be modified in-place after dereferencing it.
66+77+- The `or_insert` [method](https://doc.rust-lang.org/std/collections/hash_map/enum.Entry.html#method.or_insert) inserts the given value in the case when the entry is vacant, and returns a mutable reference to the value in the entry.
88+99+```rust
1010+*counter.entry(key).or_insert(0) += 1;
1111+```
1212+1313+- The `or_default` [method](https://doc.rust-lang.org/std/collections/hash_map/enum.Entry.html#method.or_default) ensures a value is in the entry by inserting the default value if empty, and returns a mutable reference to the value in the entry.
1414+1515+```rust
1616+*counter.entry(key).or_default() += 1;
1717+```
+44
rust/magazine-cutout/README.md
···11+# Magazine Cutout
22+33+Welcome to Magazine Cutout on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+Rust's entry API provides a view into a single entry in map, which may be either a `HashMap` or a `BTreeMap`. The entry may be either occupied and vacant, and the API provides methods to modify the contents of the entry.
1010+1111+## Instructions
1212+1313+In this exercise you'll be using a `HashMap`, along with entry API methods, to solve a simple algorithm problem.
1414+1515+Given `&[&str]` representing the words of a magazine article, and `&[&str]` representing the words of a note you would like to send, can you compose your note by cutting words out of the magazine and pasting them into a letter?
1616+1717+Notes:
1818+1919+- This implies physical cutting and pasting; the magazine needs to contain at least as many copies of each word as the note requires.
2020+- Capitalization matters; just because you're pasting together a note composed from words of a magazine doesn't mean you're willing to be ungrammatical.
2121+2222+You'll start with the following stubbed function signature:
2323+2424+```rust
2525+pub fn can_construct_note(magazine: &[&str], note: &[&str]) -> bool {
2626+ unimplemented!()
2727+}
2828+```
2929+3030+Given the following input
3131+3232+```rust
3333+let magazine = "two times three is not four".split_whitespace().collect::<Vec<&str>>();
3434+let note = "two times two is four".split_whitespace().collect::<Vec<&str>>();
3535+assert!(!can_construct_note(&magazine, ¬e));
3636+```
3737+3838+The function returns `false` since the magazine only contains one instance of `"two"` when the note requires two of them.
3939+4040+## Source
4141+4242+### Created by
4343+4444+- @seanchen1991
···11+use magazine_cutout::*;
22+33+#[test]
44+fn test_example_works() {
55+ let magazine = "two times three is not four"
66+ .split_whitespace()
77+ .collect::<Vec<&str>>();
88+ let note = "two times two is four"
99+ .split_whitespace()
1010+ .collect::<Vec<&str>>();
1111+ assert!(!can_construct_note(&magazine, ¬e));
1212+}
1313+1414+#[test]
1515+fn test_fn_returns_true_for_good_input() {
1616+ let magazine = "The metro orchestra unveiled its new grand piano today. Its donor paraphrased Nathn Hale: \"I only regret that I have but one to give \"".split_whitespace().collect::<Vec<&str>>();
1717+ let note = "give one grand today."
1818+ .split_whitespace()
1919+ .collect::<Vec<&str>>();
2020+ assert!(can_construct_note(&magazine, ¬e));
2121+}
2222+2323+#[test]
2424+fn test_fn_returns_false_for_bad_input() {
2525+ let magazine = "I've got a lovely bunch of coconuts."
2626+ .split_whitespace()
2727+ .collect::<Vec<&str>>();
2828+ let note = "I've got som coconuts"
2929+ .split_whitespace()
3030+ .collect::<Vec<&str>>();
3131+ assert!(!can_construct_note(&magazine, ¬e));
3232+}
3333+3434+#[test]
3535+fn test_case_sensitivity() {
3636+ let magazine = "i've got some lovely coconuts"
3737+ .split_whitespace()
3838+ .collect::<Vec<&str>>();
3939+ let note = "I've got some coconuts"
4040+ .split_whitespace()
4141+ .collect::<Vec<&str>>();
4242+ assert!(!can_construct_note(&magazine, ¬e));
4343+4444+ let magazine = "I've got some lovely coconuts"
4545+ .split_whitespace()
4646+ .collect::<Vec<&str>>();
4747+ let note = "i've got some coconuts"
4848+ .split_whitespace()
4949+ .collect::<Vec<&str>>();
5050+ assert!(!can_construct_note(&magazine, ¬e));
5151+}
5252+5353+#[test]
5454+fn test_magzine_has_more_than_words_available_than_needed() {
5555+ let magazine = "Enough is enough when enough is enough"
5656+ .split_whitespace()
5757+ .collect::<Vec<&str>>();
5858+ let note = "enough is enough".split_whitespace().collect::<Vec<&str>>();
5959+ assert!(can_construct_note(&magazine, ¬e));
6060+}
6161+6262+#[test]
6363+fn test_magazine_has_one_good_word_many_times_but_still_cant_construct() {
6464+ let magazine = "A A A".split_whitespace().collect::<Vec<&str>>();
6565+ let note = "A nice day".split_whitespace().collect::<Vec<&str>>();
6666+ assert!(!can_construct_note(&magazine, ¬e));
6767+}
+21
rust/resistor-color/.exercism/config.json
···11+{
22+ "blurb": "Convert a resistor band's color to its numeric representation and back, using external crates",
33+ "authors": [
44+ "still-flow",
55+ "coriolinus"
66+ ],
77+ "contributors": [],
88+ "files": {
99+ "solution": [
1010+ "src/lib.rs"
1111+ ],
1212+ "test": [
1313+ "tests/resistor-color.rs"
1414+ ],
1515+ "exemplar": [
1616+ ".meta/exemplar.rs"
1717+ ]
1818+ },
1919+ "source": "Maud de Vries, Erik Schierboom",
2020+ "source_url": "https://github.com/exercism/problem-specifications/issues/1458"
2121+}
+8
rust/resistor-color/.gitignore
···11+# Generated by Cargo
22+# will have compiled files and executables
33+/target/
44+**/*.rs.bk
55+66+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
77+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
88+Cargo.lock
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+11
rust/resistor-color/HINTS.md
···11+# Hints
22+33+## General
44+55+- Use the `enum-iterator` crate to be able to iterate over enums. Check out [docs](https://docs.rs/enum-iterator/0.7.0/enum_iterator/) to find out how.
66+77+- Use the `int-enum` crate to be able to convert from int to enum. See the [docs](https://docs.rs/int-enum/0.4.0/int_enum/) for details.
88+99+- The `Debug` trait is there because you'll likely need it.
1010+1111+- You'll need to extend the list of `derive`d traits on your colors enum.
+65
rust/resistor-color/README.md
···11+# Resistor Color
22+33+Welcome to Resistor Color on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+Most programs have dependencies on some libraries. Managing those by hand can be a pain. Luckily, the Rust ecosystem comes standard with `cargo`! `cargo` can manage dependencies for a project.
1010+1111+You will often see external packages being referred to as "crates" in Rust. A crate is a compilation unit in Rust. A crate can be compiled into a binary or into a library.
1212+1313+Most of the time, adding an external dependency is as simple as adding a line to your `Cargo.toml` file.
1414+1515+In this exercise, [`enum-iterator`](https://docs.rs/enum-iterator/0.7.0/enum_iterator/) and [`int-enum`](https://docs.rs/int-enum/0.4.0/int_enum/) dependencies were added for you.
1616+1717+## Instructions
1818+1919+If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
2020+For this exercise, you need to know two things about them:
2121+2222+- Each resistor has a resistance value.
2323+- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
2424+2525+To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
2626+Each band has a position and a numeric value.
2727+2828+The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
2929+3030+In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
3131+3232+These colors are encoded as follows:
3333+3434+- Black: 0
3535+- Brown: 1
3636+- Red: 2
3737+- Orange: 3
3838+- Yellow: 4
3939+- Green: 5
4040+- Blue: 6
4141+- Violet: 7
4242+- Grey: 8
4343+- White: 9
4444+4545+The goal of this exercise is to create a way:
4646+- to look up the numerical value associated with a particular color band
4747+- to convert the numerical value into a string representing color
4848+- to list the different band colors
4949+5050+For tasks number two and three, you will need external crates [`enum-iterator`](https://docs.rs/enum-iterator/0.7.0/enum_iterator/) and [`int-enum`](https://docs.rs/int-enum/0.4.0/int_enum/), which are included in this exercise's `Cargo.toml`. Be sure to check the crates' documentation to learn how to use them.
5151+5252+Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: Better Be Right Or Your Great Big Values Go Wrong.
5353+5454+More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article](https://en.wikipedia.org/wiki/Electronic_color_code)
5555+5656+## Source
5757+5858+### Created by
5959+6060+- @still-flow
6161+- @coriolinus
6262+6363+### Based on
6464+6565+Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1458
+34
rust/resistor-color/src/lib.rs
···11+22+use int_enum::IntEnum;
33+use enum_iterator::IntoEnumIterator;
44+55+#[repr(usize)]
66+#[derive(Clone, Copy, Debug, PartialEq, IntEnum, IntoEnumIterator)]
77+pub enum ResistorColor {
88+ Black = 0,
99+ Brown = 1,
1010+ Red = 2,
1111+ Orange = 3,
1212+ Yellow = 4,
1313+ Green = 5,
1414+ Blue = 6,
1515+ Violet = 7,
1616+ Grey = 8,
1717+ White = 9,
1818+}
1919+2020+pub fn color_to_value(color: ResistorColor) -> usize {
2121+ color.int_value()
2222+}
2323+2424+pub fn value_to_color_string(value: usize) -> String {
2525+ let x = ResistorColor::from_int(value);
2626+ match x{
2727+ Ok(v) => format!("{:?}", v),
2828+ Err(_) => "value out of range".to_string()
2929+ }
3030+}
3131+3232+pub fn colors() -> Vec<ResistorColor> {
3333+ ResistorColor::into_enum_iter().collect()
3434+}
···11+# This file is automatically @generated by Cargo.
22+# It is not intended for manual editing.
33+version = 3
44+55+[[package]]
66+name = "role_playing_game"
77+version = "0.1.0"
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
···11+# Role-Playing Game
22+33+Welcome to Role-Playing Game on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+## Instructions
1010+1111+You're working on implementing a role-playing game. The player's character is represented by the following:
1212+1313+```rust
1414+pub struct Player {
1515+ health: u32,
1616+ mana: Option<u32>,
1717+ level: u32,
1818+}
1919+```
2020+2121+Players in this game must reach level 10 before they unlock a mana pool so that they can start casting spells. Before that point, the Player's mana is `None`.
2222+2323+You're working on two pieces of functionality in this game, the revive mechanic and the spell casting mechanic.
2424+2525+The `revive` method should check to ensure that the Player is indeed dead (their health has reached 0), and if they are, the method should return a new Player instance with 100 health.
2626+If the Player's level is 10 or above, they should also be revived with 100 mana.
2727+If the Player's level is below 10, their mana should be `None`. The `revive` method should preserve the Player's level.
2828+2929+```rust
3030+let dead_player = Player { health: 0, mana: None, level: 2 };
3131+dead_player.revive()
3232+// Returns Player { health: 100, mana: None, level: 2 }
3333+```
3434+3535+If the `revive` method is called on a Player whose health is 1 or above, then the method should return `None`.
3636+3737+```rust
3838+let alive_player = Player { health: 1, mana: Some(15), level: 11 };
3939+alive_player.revive()
4040+// Returns None
4141+```
4242+4343+The `cast_spell` method takes a mutable reference to the Player as well as a `mana_cost` parameter indicating how much mana the spell costs. It returns the amount of damage that the cast spell performs, which will always be two times the mana cost of the spell if the spell is successfully cast.
4444+4545+- If the player does not have access to a mana pool, attempting to cast the spell must decrease their health by the mana cost of the spell. The damage returned must be 0.
4646+4747+ ```rust
4848+ let not_a_wizard_yet = Player { health: 79, mana: None, level: 9 };
4949+ assert_eq!(not_a_wizard_yet.cast_spell(5), 0)
5050+ assert_eq!(not_a_wizard_yet.health, 74);
5151+ assert_eq!(not_a_wizard_yet.mana, None);
5252+ ```
5353+5454+- If the player has a mana pool but insufficient mana, the method should not affect the pool, but instead return 0
5555+5656+ ```rust
5757+ let low_mana_wizard = Player { health: 93, mana: Some(3), level: 12 };
5858+ assert_eq!(low_mana_wizard.cast_spell(10), 0);
5959+ assert_eq!(low_mana_wizard.health, 93);
6060+ assert_eq!(low_mana_wizard.mana, Some(3));
6161+ ```
6262+6363+- Otherwise, the `mana_cost` should be deducted from the Player's mana pool and the appropriate amount of damage should be returned.
6464+6565+ ```rust
6666+ let wizard = Player { health: 123, mana: Some(30), level: 18 };
6767+ assert_eq!(wizard.cast_spell(10), 20);
6868+ assert_eq!(wizard.health, 123);
6969+ assert_eq!(wizard.mana, Some(20));
7070+ ```
7171+7272+Have fun!
7373+7474+## Source
7575+7676+### Created by
7777+7878+- @seanchen1991
7979+- @coriolinus
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+5
rust/rpn-calculator/HINTS.md
···11+# Hints
22+33+## General
44+55+- Look at the documentation for `std::vec::Vec`'s [`push()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.push) and ['pop()`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.pop) methods.
+147
rust/rpn-calculator/README.md
···11+# RPN Calculator
22+33+Welcome to RPN Calculator on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+[Stacks](https://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29) are a type of collection commonly used in computer science.
1010+They are defined by their two key operations: **push** and **pop**.
1111+**Push** adds an element to the top of the stack.
1212+**Pop** removes and returns the topmost element.
1313+1414+Think of a stack like a stack of plates.
1515+You can either add a plate to the top of the stack or take the topmost plate.
1616+To access something further down, you have to remove all of the plates above it.
1717+1818+Rust's vector implementation, [`std::vec::Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html), can be used as a stack by using its `push()` and `pop()` methods.
1919+Naturally, `push()` adds an element to the end of the `Vec` and `pop()` removes and returns the last element.
2020+These operation can be very fast (O(1) in [Big O Notation](https://en.wikipedia.org/wiki/Big_O_notation)),
2121+so they are one of the most idiomatic ways to use a `Vec`.
2222+2323+Stacks are useful to hold arbitrary numbers of elements in a specific order.
2424+Because the last element inserted is the first element returned,
2525+stacks are commonly refered to as **LIFO** (Last-In, First-Out).
2626+This inherent ordering can be used for many things,
2727+including tracking state when evaulating **Reverse Polish notation**.
2828+2929+## Instructions
3030+3131+## 1. Overview
3232+3333+[Reverse Polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation) (RPN) is a way of writing mathematical expressions.
3434+Unlike in traditional infix notation, RPN operators *follow* their operands.
3535+For example, instead of writing:
3636+3737+```
3838+2 + 2
3939+```
4040+4141+you would write:
4242+4343+```
4444+2 2 +
4545+```
4646+4747+The major benefit of Reverse Polish notation is that it is much simpler to parse than infix notation.
4848+RPN eliminates the need for order of operations or parentheses in complex expressions.
4949+For example:
5050+5151+```
5252+(4 + 8) / (7 - 5)
5353+```
5454+5555+can be written as
5656+5757+```
5858+4 8 + 7 5 - /
5959+```
6060+6161+In both cases, the expression evaluates to 6.
6262+6363+## 2. Example
6464+6565+Lets manually evaluate that complex expression.
6666+As we learned in the introduction, evaluation of RPN requires a stack.
6767+This stack is used to hold numeric values that the operators operate on.
6868+We start our calculator with an empty stack and then evaluate each element one at a time.
6969+7070+First, we encounter a `4`,
7171+so we push it onto our freshly created stack.
7272+7373+```
7474+4
7575+```
7676+7777+Next, we encounter an `8`.
7878+We also push that onto the stack.
7979+8080+```
8181+4 8
8282+```
8383+8484+Now, we encounter a `+`.
8585+We pop off the two topmost values (4 and 8),
8686+add them together,
8787+and push the sum back onto the stack.
8888+8989+```
9090+12
9191+```
9292+9393+We do something similar for `7`, `5`, and `-`:
9494+9595+```
9696+12 7
9797+12 7 5
9898+12 2
9999+```
100100+101101+Now we encounter a `/`.
102102+Even though we last encountered a `-`,
103103+there are two elements on the stack.
104104+We pop off the two elements,
105105+divide them,
106106+and push the result back onto the stack.
107107+108108+```
109109+6
110110+```
111111+112112+Finally, since there is exactly one element on the stack,
113113+we can say the expression evaluated to 6.
114114+115115+## 3. Goal
116116+117117+Your goal is to write a calculator to evaluate a list of inputs ordered by Reverse Polish notation and return the final element on the stack.
118118+119119+If there is not exactly one element in the stack at the end, return `None`.
120120+121121+If there is an operator with too few operands (such as the input `2 +`), return `None`.
122122+123123+You are given the following enum and stubbed function as a starting point.
124124+125125+```rust
126126+#[derive(Debug)]
127127+pub enum CalculatorInput {
128128+ Add,
129129+ Subtract,
130130+ Multiply,
131131+ Divide,
132132+ Value(i32),
133133+}
134134+135135+pub fn evaluate(inputs: &[CalculatorInput]) -> Option<i32> {
136136+ unimplemented!(
137137+ "Given the inputs: {:?}, evaluate them as though they were a Reverse Polish notation expression",
138138+ inputs,
139139+ );
140140+}
141141+```
142142+143143+## Source
144144+145145+### Created by
146146+147147+- @cwhakes
+15
rust/rpn-calculator/src/lib.rs
···11+#[derive(Debug)]
22+pub enum CalculatorInput {
33+ Add,
44+ Subtract,
55+ Multiply,
66+ Divide,
77+ Value(i32),
88+}
99+1010+pub fn evaluate(inputs: &[CalculatorInput]) -> Option<i32> {
1111+ unimplemented!(
1212+ "Given the inputs: {:?}, evaluate them as though they were a Reverse Polish notation expression",
1313+ inputs,
1414+ );
1515+}
···11+# This file is automatically @generated by Cargo.
22+# It is not intended for manual editing.
33+version = 3
44+55+[[package]]
66+name = "semi_structured_logs"
77+version = "0.1.0"
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+13
rust/semi-structured-logs/HINTS.md
···11+# Hints
22+33+## General
44+55+- [Rust By Example - Enums][rbe-enums]
66+- [cheats.rs - Basic Types][cheats-types]
77+88+## 1. Emit semi-structured messages
99+1010+- `match` comes in handy when working with enums. In this case, see how you might use it when figuring how what kind of log message to generate.
1111+1212+[rbe-enums]: https://doc.rust-lang.org/stable/rust-by-example/custom_types/enum.html#enums
1313+[cheats-types]: https://cheats.rs/#basic-types
+64
rust/semi-structured-logs/README.md
···11+# Semi Structured Logs
22+33+Welcome to Semi Structured Logs on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+Enums, short for enumerations, are a type that limits all possible values of some data. The possible values of an `enum` are called variants. Enums also work well with `match` and other control flow operators to help you express intent in your Rust programs.
1010+1111+## Instructions
1212+1313+In this exercise you'll be generating semi-structured log messages.
1414+1515+## 1. Emit semi-structured messages
1616+1717+You'll start with some stubbed functions and the following enum:
1818+1919+```rust
2020+#[derive(Clone, PartialEq, Debug)]
2121+pub enum LogLevel {
2222+ Info,
2323+ Warning,
2424+ Error,
2525+}
2626+```
2727+2828+Your goal is to emit a log message as follows: `"[<LEVEL>]: <MESSAGE>"`.
2929+You'll need to implement functions that correspond with log levels.
3030+3131+For example, the below snippet demonstrates an expected output for the `log` function.
3232+3333+```rust
3434+log(LogLevel::Error, "Stack overflow")
3535+// Returns: "[ERROR]: Stack overflow"
3636+```
3737+3838+And for `info`:
3939+4040+```rust
4141+info("Timezone changed")
4242+// Returns: "[INFO]: Timezone changed"
4343+```
4444+4545+Have fun!
4646+4747+## 2. Optional further practice
4848+4949+There is a feature-gated test in this suite.
5050+Feature gates disable compilation entirely for certain sections of your program.
5151+They will be covered later.
5252+For now just know that there is a test which is only built and run when you use a special testing invocation:
5353+5454+```sh
5555+cargo test --features add-a-variant
5656+```
5757+5858+This test is meant to show you how to add a variant to your enum.
5959+6060+## Source
6161+6262+### Created by
6363+6464+- @efx
···11+# Generated by Cargo
22+# will have compiled files and executables
33+/target/
44+**/*.rs.bk
55+66+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
77+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
88+Cargo.lock
···11+# Help
22+33+## Running the tests
44+55+Execute the tests with:
66+77+```bash
88+$ cargo test
99+```
1010+1111+All but the first test have been ignored. After you get the first test to
1212+pass, open the tests source file which is located in the `tests` directory
1313+and remove the `#[ignore]` flag from the next test and get the tests to pass
1414+again. Each separate test is a function with `#[test]` flag above it.
1515+Continue, until you pass every test.
1616+1717+If you wish to run _only ignored_ tests without editing the tests source file, use:
1818+1919+```bash
2020+$ cargo test -- --ignored
2121+```
2222+2323+If you are using Rust 1.51 or later, you can run _all_ tests with
2424+2525+```bash
2626+$ cargo test -- --include-ignored
2727+```
2828+2929+To run a specific test, for example `some_test`, you can use:
3030+3131+```bash
3232+$ cargo test some_test
3333+```
3434+3535+If the specific test is ignored, use:
3636+3737+```bash
3838+$ cargo test some_test -- --ignored
3939+```
4040+4141+To learn more about Rust tests refer to the online [test documentation][rust-tests].
4242+4343+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
4444+4545+## Submitting your solution
4646+4747+You can submit your solution using the `exercism submit src/lib.rs` command.
4848+This command will upload your solution to the Exercism website and print the solution page's URL.
4949+5050+It's possible to submit an incomplete solution which allows you to:
5151+5252+- See how others have completed the exercise
5353+- Request help from a mentor
5454+5555+## Need to get help?
5656+5757+If you'd like help solving the exercise, check the following pages:
5858+5959+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
6060+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
6161+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
6262+6363+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
6464+6565+## Rust Installation
6666+6767+Refer to the [exercism help page][help-page] for Rust installation and learning
6868+resources.
6969+7070+## Submitting the solution
7171+7272+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.
7373+7474+## Feedback, Issues, Pull Requests
7575+7676+The GitHub [track repository][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!
7777+7878+If you want to know more about Exercism, take a look at the [contribution guide].
7979+8080+## Submitting Incomplete Solutions
8181+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
8282+8383+[help-page]: https://exercism.io/tracks/rust/learning
8484+[github]: https://github.com/exercism/rust
8585+[contribution guide]: https://exercism.io/docs/community/contributors
+5
rust/short-fibonacci/HINTS.md
···11+# Hints
22+33+## General
44+55+- Check out the Rust documentation on [the `vec!` macro](https://doc.rust-lang.org/std/macro.vec.html).
+40
rust/short-fibonacci/README.md
···11+# A Short Fibonacci Sequence
22+33+Welcome to A Short Fibonacci Sequence on Exercism's Rust Track.
44+If you need help running the tests or submitting your code, check out `HELP.md`.
55+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
66+77+## Introduction
88+99+Rust provides a macro `vec![]` to help you create Vectors.
1010+This comes in quite handy when you need to initialize lists.
1111+1212+## Instructions
1313+1414+You are going to initialize empty buffers and list the first five numbers, or elements, of the Fibonacci sequence.
1515+1616+The Fibonacci sequence is a set of numbers where the next element is the sum of the prior two. We start the sequence at one. So the first two elements are 1 and 1.
1717+1818+## 1. Create a buffer of `count` zeroes.
1919+2020+Create a function that creates a buffer of `count` zeroes.
2121+```rust
2222+let my_buffer = create_buffer(5);
2323+// [0, 0, 0, 0, 0]
2424+```
2525+2626+## 2. List the first five elements of the Fibonacci sequence
2727+2828+Create a function that returns the first five numbers of the Fibonacci sequence.
2929+Its first five elements are `1, 1, 2, 3, 5`
3030+```rust
3131+let first_five = fibonacci();
3232+// [1, 1, 2, 3, 5]
3333+```
3434+3535+## Source
3636+3737+### Created by
3838+3939+- @efx
4040+- @coriolinus
+25
rust/short-fibonacci/src/lib.rs
···11+/// Create an empty vector
22+pub fn create_empty() -> Vec<u8> {
33+ vec![]
44+}
55+66+/// Create a buffer of `count` zeroes.
77+///
88+/// Applications often use buffers when serializing data to send over the network.
99+pub fn create_buffer(count: usize) -> Vec<u8> {
1010+ vec![0; count]
1111+}
1212+1313+/// Create a vector containing the first five elements of the Fibonacci sequence.
1414+///
1515+/// Fibonacci's sequence is the list of numbers where the next number is a sum of the previous two.
1616+/// Its first five elements are `1, 1, 2, 3, 5`.
1717+pub fn fibonacci() -> Vec<u8> {
1818+ create_buffer(5)
1919+ .iter()
2020+ .fold((create_empty(), 0, 1), |(mut acc, a, b), _| {
2121+ acc.push(b);
2222+ (acc, b, a + b)
2323+ })
2424+ .0
2525+}
+26
rust/short-fibonacci/tests/short-fibonacci.rs
···11+use short_fibonacci::*;
22+33+#[test]
44+fn test_empty() {
55+ assert_eq!(create_empty(), Vec::new());
66+}
77+#[test]
88+#[ignore]
99+fn test_buffer() {
1010+ for n in 0..10 {
1111+ let zeroized = create_buffer(n);
1212+ assert_eq!(zeroized.len(), n);
1313+ assert!(zeroized.iter().all(|&v| v == 0));
1414+ }
1515+}
1616+#[test]
1717+#[ignore]
1818+fn test_fibonacci() {
1919+ let fibb = fibonacci();
2020+ assert_eq!(fibb.len(), 5);
2121+ assert_eq!(fibb[0], 1);
2222+ assert_eq!(fibb[1], 1);
2323+ for window in fibb.windows(3) {
2424+ assert_eq!(window[0] + window[1], window[2]);
2525+ }
2626+}