···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
+66
rust/macros/README.md
···11+# Macros
22+33+Welcome to Macros 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+Macros are a powerful part of a Rust programmer's toolkit, and [macros by example](https://doc.rust-lang.org/reference/macros-by-example.html) are a relatively simple way to access this power. Let's write one!
99+1010+## Context
1111+1212+What is a macro? [Wikipedia](https://en.wikipedia.org/wiki/Macro_(computer_science)) describes it thus:
1313+1414+> A macro (short for "macroinstruction", from Greek μακρός 'long') in computer science is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to a replacement output sequence (also often a sequence of characters) according to a defined procedure. The mapping process that instantiates (transforms) a macro use into a specific sequence is known as macro expansion.
1515+1616+Illuminating! But to be more concrete, macros are a special syntax which allows you to generate code at compile time. Macros can be used for compile-time calculation, but more often they're just another way to abstract your code. For example, you've probably already used `println!()` and `vec![]`. These each take an arbitrary number of arguments, so you can't express them as simple functions. On the other hand, they always expand to some amount of absolutely standard Rust code. If you're interested, you can use the [cargo expand](https://github.com/dtolnay/cargo-expand) subcommand to view the results of macro expansion in your code.
1717+1818+For further information about macros in Rust, The Rust Book has a [good chapter](https://doc.rust-lang.org/book/ch19-06-macros.html) on them.
1919+2020+## Problem Statement
2121+2222+You can produce a `Vec` of arbitrary length inline by using the `vec![]` macro. However, Rust doesn't come with a way to produce a [`HashMap`](https://doc.rust-lang.org/std/collections/struct.HashMap.html) inline. Rectify this by writing a `hashmap!()` macro.
2323+2424+For example, a user of your library might write `hashmap!('a' => 3, 'b' => 11, 'z' => 32)`. This should expand to the following code:
2525+2626+```rust
2727+{
2828+ let mut hm = HashMap::new();
2929+ hm.insert('a', 3);
3030+ hm.insert('b', 11);
3131+ hm.insert('z', 32);
3232+ hm
3333+}
3434+```
3535+3636+Note that the [`maplit` crate](https://crates.io/crates/maplit) provides a macro which perfectly solves this exercise. Please implement your own solution instead of using this crate; please make an attempt on your own before viewing its source.
3737+3838+Note that this exercise requires Rust 1.36 or later.
3939+4040+## Source
4141+4242+### Created by
4343+4444+- @coriolinus
4545+4646+### Contributed to by
4747+4848+- @bantic
4949+- @cwhakes
5050+- @DarthStrom
5151+- @efx
5252+- @Emerentius
5353+- @ErikSchierboom
5454+- @lutostag
5555+- @pedantic79
5656+- @petertseng
5757+- @rofrol
5858+- @ssomers
5959+- @stringparser
6060+- @tjade273
6161+- @xakon
6262+- @ZapAnton
6363+6464+### Based on
6565+6666+Peter Goodspeed-Niklaus
···11+#
22+# This Cargo.toml file is used by the simple-trybuild module.
33+# When adding a new file, please name the [[bin]] name to match the file
44+# it is used to produce an error message
55+#
66+77+[package]
88+name = "macros-tests"
99+version = "0.0.0"
1010+edition = "2021"
1111+publish = false
1212+1313+[dependencies.macros]
1414+path = "../../"
1515+default-features = false
1616+1717+[[bin]]
1818+name = "comma-sep-rs"
1919+path = "comma-sep.rs"
2020+2121+[[bin]]
2222+name = "double-commas-rs"
2323+path = "double-commas.rs"
2424+2525+[[bin]]
2626+name = "only-arrow-rs"
2727+path = "only-arrow.rs"
2828+2929+[[bin]]
3030+name = "only-comma-rs"
3131+path = "only-comma.rs"
3232+3333+[[bin]]
3434+name = "single-argument-rs"
3535+path = "single-argument.rs"
3636+3737+[[bin]]
3838+name = "triple-arguments-rs"
3939+path = "triple-arguments.rs"
4040+4141+[[bin]]
4242+name = "two-arrows-rs"
4343+path = "two-arrows.rs"
4444+4545+[[bin]]
4646+name = "leading-comma-rs"
4747+path = "leading-comma.rs"
4848+4949+[[bin]]
5050+name = "no-comma-rs"
5151+path = "no-comma.rs"
5252+5353+[[bin]]
5454+name = "missing-argument-rs"
5555+path = "missing-argument.rs"
+7
rust/macros/tests/invalid/comma-sep.rs
···11+use macros::hashmap;
22+use std::collections::HashMap;
33+44+fn main() {
55+ // using only commas is invalid
66+ let _hm: HashMap<_, _> = hashmap!('a', 1);
77+}
+7
rust/macros/tests/invalid/double-commas.rs
···11+use macros::hashmap;
22+use std::collections::HashMap;
33+44+fn main() {
55+ // a single trailing comma is okay, but two is not
66+ let _hm: HashMap<_, _> = hashmap!('a' => 2, ,);
77+}
+7
rust/macros/tests/invalid/leading-comma.rs
···11+use macros::hashmap;
22+use std::collections::HashMap;
33+44+fn main() {
55+ // leading commas are not valid
66+ let _hm: HashMap<_, _> = hashmap!(, 'a' => 2);
77+}
+7
rust/macros/tests/invalid/missing-argument.rs
···11+use macros::hashmap;
22+use std::collections::HashMap;
33+44+fn main() {
55+ // an argument should come between each pair of commas
66+ let _hm: HashMap<_, _> = hashmap!('a' => 1, , 'b' => 2);
77+}
+7
rust/macros/tests/invalid/no-comma.rs
···11+use macros::hashmap;
22+use std::collections::HashMap;
33+44+fn main() {
55+ // Key value pairs must be separated by commas
66+ let _hm: HashMap<_, _> = hashmap!('a' => 1 'b' => 2);
77+}
+7
rust/macros/tests/invalid/only-arrow.rs
···11+use macros::hashmap;
22+use std::collections::HashMap;
33+44+fn main() {
55+ // a single random arrow is not valid
66+ let _hm: HashMap<(), ()> = hashmap!(=>);
77+}
+7
rust/macros/tests/invalid/only-comma.rs
···11+use macros::hashmap;
22+use std::collections::HashMap;
33+44+fn main() {
55+ // a single random comma is not valid
66+ let _hm: HashMap<(), ()> = hashmap!(,);
77+}
+7
rust/macros/tests/invalid/single-argument.rs
···11+use macros::hashmap;
22+use std::collections::HashMap;
33+44+fn main() {
55+ // a single argument is invalid
66+ let _hm: HashMap<_, _> = hashmap!('a');
77+}
+7
rust/macros/tests/invalid/triple-arguments.rs
···11+use macros::hashmap;
22+use std::collections::HashMap;
33+44+fn main() {
55+ // three arguments are invalid
66+ hashmap!('a' => 1, 'b');
77+}