···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
+102
rust/luhn/README.md
···11+# Luhn
22+33+Welcome to Luhn 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+Given a number determine whether or not it is valid per the Luhn formula.
99+1010+The [Luhn algorithm](https://en.wikipedia.org/wiki/Luhn_algorithm) is
1111+a simple checksum formula used to validate a variety of identification
1212+numbers, such as credit card numbers and Canadian Social Insurance
1313+Numbers.
1414+1515+The task is to check if a given string is valid.
1616+1717+Validating a Number
1818+------
1919+2020+Strings of length 1 or less are not valid. Spaces are allowed in the input,
2121+but they should be stripped before checking. All other non-digit characters
2222+are disallowed.
2323+2424+## Example 1: valid credit card number
2525+2626+```text
2727+4539 3195 0343 6467
2828+```
2929+3030+The first step of the Luhn algorithm is to double every second digit,
3131+starting from the right. We will be doubling
3232+3333+```text
3434+4_3_ 3_9_ 0_4_ 6_6_
3535+```
3636+3737+If doubling the number results in a number greater than 9 then subtract 9
3838+from the product. The results of our doubling:
3939+4040+```text
4141+8569 6195 0383 3437
4242+```
4343+4444+Then sum all of the digits:
4545+4646+```text
4747+8+5+6+9+6+1+9+5+0+3+8+3+3+4+3+7 = 80
4848+```
4949+5050+If the sum is evenly divisible by 10, then the number is valid. This number is valid!
5151+5252+## Example 2: invalid credit card number
5353+5454+```text
5555+8273 1232 7352 0569
5656+```
5757+5858+Double the second digits, starting from the right
5959+6060+```text
6161+7253 2262 5312 0539
6262+```
6363+6464+Sum the digits
6565+6666+```text
6767+7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57
6868+```
6969+7070+57 is not evenly divisible by 10, so this number is not valid.
7171+7272+## Source
7373+7474+### Created by
7575+7676+- @IanWhitney
7777+7878+### Contributed to by
7979+8080+- @AvasDream
8181+- @bitfield
8282+- @coriolinus
8383+- @cwhakes
8484+- @efx
8585+- @ErikSchierboom
8686+- @gibfahn
8787+- @idealhack
8888+- @lutostag
8989+- @mkantor
9090+- @navossoc
9191+- @nfiles
9292+- @petertseng
9393+- @rofrol
9494+- @stkent
9595+- @stringparser
9696+- @workingjubilee
9797+- @xakon
9898+- @ZapAnton
9999+100100+### Based on
101101+102102+The Luhn Algorithm on Wikipedia - http://en.wikipedia.org/wiki/Luhn_algorithm
+100
rust/luhn/src/lib.rs
···11+fn dbl_and_check(x: u32) -> u32 {
22+ let i = x * 2;
33+ if i > 9 {
44+ i - 9
55+ } else {
66+ i
77+ }
88+}
99+1010+#[derive(Debug, Clone)]
1111+struct Input(Vec<char>, usize);
1212+1313+impl From<&str> for Input {
1414+ fn from(x: &str) -> Self {
1515+ Input(x.chars().rev().collect(), 0)
1616+ }
1717+}
1818+1919+impl Iterator for Input {
2020+ type Item = Parsed;
2121+2222+ fn next(&mut self) -> Option<Self::Item> {
2323+ let len = self.0.len();
2424+ match self.1 {
2525+ x if x < len => {
2626+ let this = self.0.get(x);
2727+ match this {
2828+ Some(sp) if (*sp).is_whitespace() => {
2929+ self.1 += 1;
3030+ Some(Parsed::Space)
3131+ }
3232+ Some(t) => (|x| {
3333+ self.1 += 1;
3434+ match x {
3535+ Some(v) => Some(Parsed::Digit(v)),
3636+ None => Some(Parsed::SomethingElse),
3737+ }
3838+ })(t.to_digit(10)),
3939+ None => None,
4040+ }
4141+ }
4242+ _ => None,
4343+ }
4444+ }
4545+}
4646+4747+#[derive(Debug)]
4848+enum Parsed {
4949+ Digit(u32),
5050+ Space,
5151+ SomethingElse,
5252+}
5353+5454+impl Parsed {
5555+ fn is_something_else(&self) -> bool {
5656+ match self {
5757+ Parsed::SomethingElse => true,
5858+ _ => false,
5959+ }
6060+ }
6161+6262+ fn is_digit(&self) -> bool {
6363+ match self {
6464+ Parsed::Digit(_) => true,
6565+ _ => false,
6666+ }
6767+ }
6868+6969+ fn get_digit(&self) -> Option<u32> {
7070+ match self {
7171+ Parsed::Digit(x) => Some(*x),
7272+ Parsed::Space => None,
7373+ _ => None,
7474+ }
7575+ }
7676+}
7777+7878+/// Check a Luhn checksum.
7979+pub fn is_valid(code: &str) -> bool {
8080+ let inp: Input = Input::from(code);
8181+ let has_non_digit_non_space = inp.clone().any(|x| x.is_something_else());
8282+ let only_digits = inp
8383+ .clone()
8484+ .filter(|x| x.is_digit())
8585+ .map_while(|x| x.get_digit());
8686+8787+ if has_non_digit_non_space || only_digits.clone().count() <= 1 {
8888+ false
8989+ } else {
9090+ let y: u32 = only_digits
9191+ .clone()
9292+ .enumerate()
9393+ .map(|(id, el)| match id {
9494+ _ if id % 2 != 0 => dbl_and_check(el),
9595+ _ => el,
9696+ })
9797+ .sum();
9898+ y % 10 == 0
9999+ }
100100+}
+132
rust/luhn/tests/luhn.rs
···11+use luhn::*;
22+33+fn process_valid_case(number: &str, is_luhn_expected: bool) {
44+ assert_eq!(is_valid(number), is_luhn_expected);
55+}
66+77+#[test]
88+fn test_single_digit_strings_can_not_be_valid() {
99+ process_valid_case("1", false);
1010+}
1111+1212+#[test]
1313+#[ignore]
1414+fn test_a_single_zero_is_invalid() {
1515+ process_valid_case("0", false);
1616+}
1717+1818+#[test]
1919+#[ignore]
2020+fn test_a_simple_valid_sin_that_remains_valid_if_reversed() {
2121+ process_valid_case("059", true);
2222+}
2323+2424+#[test]
2525+fn test_a_simple_valid_sin_that_becomes_invalid_if_reversed() {
2626+ process_valid_case("59", true);
2727+}
2828+2929+#[test]
3030+#[ignore]
3131+fn test_a_valid_canadian_sin() {
3232+ process_valid_case("055 444 285", true);
3333+}
3434+3535+#[test]
3636+#[ignore]
3737+fn test_invalid_canadian_sin() {
3838+ process_valid_case("055 444 286", false);
3939+}
4040+4141+#[test]
4242+#[ignore]
4343+fn test_invalid_credit_card() {
4444+ process_valid_case("8273 1232 7352 0569", false);
4545+}
4646+4747+#[test]
4848+#[ignore]
4949+fn test_valid_number_with_an_even_number_of_digits() {
5050+ process_valid_case("095 245 88", true);
5151+}
5252+5353+#[test]
5454+fn strings_that_contain_non_digits_are_invalid() {
5555+ process_valid_case("055a 444 285", false);
5656+}
5757+5858+#[test]
5959+#[ignore]
6060+fn test_valid_strings_with_punctuation_included_become_invalid() {
6161+ process_valid_case("055-444-285", false);
6262+}
6363+6464+#[test]
6565+#[ignore]
6666+fn symbols_are_invalid() {
6767+ process_valid_case("055£ 444$ 285", false);
6868+}
6969+7070+#[test]
7171+#[ignore]
7272+fn test_single_zero_with_space_is_invalid() {
7373+ process_valid_case(" 0", false);
7474+}
7575+7676+#[test]
7777+#[ignore]
7878+fn test_more_than_a_single_zero_is_valid() {
7979+ process_valid_case("0000 0", true);
8080+}
8181+8282+#[test]
8383+#[ignore]
8484+fn test_input_digit_9_is_correctly_converted_to_output_digit_9() {
8585+ process_valid_case("091", true);
8686+}
8787+8888+#[test]
8989+#[ignore]
9090+/// using ascii value for doubled non-digit isn't allowed
9191+/// Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid.
9292+/// This test is designed to avoid that solution.
9393+fn test_using_ascii_value_for_doubled_nondigit_isnt_allowed() {
9494+ process_valid_case(":9", false);
9595+}
9696+9797+#[test]
9898+#[ignore]
9999+/// valid strings with a non-digit added at the end become invalid
100100+fn test_valid_strings_with_a_nondigit_added_at_the_end_become_invalid() {
101101+ process_valid_case("059a", false);
102102+}
103103+104104+#[test]
105105+#[ignore]
106106+/// valid strings with symbols included become invalid
107107+fn test_valid_strings_with_symbols_included_become_invalid() {
108108+ process_valid_case("055# 444$ 285", false);
109109+}
110110+111111+#[test]
112112+#[ignore]
113113+/// using ascii value for non-doubled non-digit isn't allowed
114114+/// Convert non-digits to their ascii values and then offset them by 48 sometimes accidentally declare an invalid string to be valid.
115115+/// This test is designed to avoid that solution.
116116+fn test_using_ascii_value_for_nondoubled_nondigit_isnt_allowed() {
117117+ process_valid_case("055b 444 285", false);
118118+}
119119+120120+#[test]
121121+#[ignore]
122122+/// valid number with an odd number of spaces
123123+fn test_valid_number_with_an_odd_number_of_spaces() {
124124+ process_valid_case("234 567 891 234", true);
125125+}
126126+127127+#[test]
128128+#[ignore]
129129+/// non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed
130130+fn test_invalid_char_in_middle_with_sum_divisible_by_10_isnt_allowed() {
131131+ process_valid_case("59%59", false);
132132+}