Before doing this exercise you should probably do the original Luhn exercise. If you have not completed Luhn, you can get it by running the command:
exercism download --exercise=luhn --track=rust
In the original Luhn exercise you only validated strings, but the Luhn algorithm can be applied to integers as well.
In this exercise you'll implement the From trait to convert strings, strs and unsigned integers into a Struct that performs the validation.
Refer to the exercism help page for Rust installation and learning resources.
Execute the tests with:
$ cargo test
All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the tests
directory
and remove the #[ignore]
flag from the next test and get the tests to pass
again. Each separate test is a function with #[test]
flag above it.
Continue, until you pass every test.
If you wish to run all tests without editing the tests source file, use:
$ cargo test -- --ignored
To run a specific test, for example some_test
, you can use:
$ cargo test some_test
If the specific test is ignored use:
$ cargo test some_test -- --ignored
To learn more about Rust tests refer to the online test documentation
Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.
The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
If you want to know more about Exercism, take a look at the contribution guide.
The Rust track maintainers, based on the original Luhn exercise
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
extern crate luhn_from;
use luhn_from::*;
#[test]
fn you_can_validate_from_a_str() {
let valid = Luhn::from("046 454 286");
let invalid = Luhn::from("046 454 287");
assert!(valid.is_valid());
assert!(!invalid.is_valid());
}
#[test]
#[ignore]
fn you_can_validate_from_a_string() {
let valid = Luhn::from(String::from("046 454 286"));
let invalid = Luhn::from(String::from("046 454 287"));
assert!(valid.is_valid());
assert!(!invalid.is_valid());
}
#[test]
#[ignore]
fn you_can_validate_from_a_u8() {
let valid = Luhn::from(240u8);
let invalid = Luhn::from(241u8);
assert!(valid.is_valid());
assert!(!invalid.is_valid());
}
#[test]
#[ignore]
fn you_can_validate_from_a_u16() {
let valid = Luhn::from(64_436u16);
let invalid = Luhn::from(64_437u16);
assert!(valid.is_valid());
assert!(!invalid.is_valid());
}
#[test]
#[ignore]
fn you_can_validate_from_a_u32() {
let valid = Luhn::from(46_454_286u32);
let invalid = Luhn::from(46_454_287u32);
assert!(valid.is_valid());
assert!(!invalid.is_valid());
}
#[test]
#[ignore]
fn you_can_validate_from_a_u64() {
let valid = Luhn::from(8273_1232_7352_0562u64);
let invalid = Luhn::from(8273_1232_7352_0569u64);
assert!(valid.is_valid());
assert!(!invalid.is_valid());
}
#[test]
#[ignore]
fn you_can_validate_from_a_usize() {
let valid = Luhn::from(8273_1232_7352_0562usize);
let invalid = Luhn::from(8273_1232_7352_0569usize);
assert!(valid.is_valid());
assert!(!invalid.is_valid());
}
#[test]
#[ignore]
fn single_digit_string_is_invalid() {
assert!(!Luhn::from("1").is_valid());
}
#[test]
#[ignore]
fn single_zero_string_is_invalid() {
assert!(!Luhn::from("0").is_valid());
}
#[test]
#[ignore]
fn valid_canadian_sin_is_valid() {
assert!(Luhn::from("046 454 286").is_valid());
}
#[test]
#[ignore]
fn invalid_canadian_sin_is_invalid() {
assert!(!Luhn::from("046 454 287").is_valid());
}
#[test]
#[ignore]
fn invalid_credit_card_is_invalid() {
assert!(!Luhn::from("8273 1232 7352 0569").is_valid());
}
#[test]
#[ignore]
fn strings_that_contain_non_digits_are_invalid() {
assert!(!Luhn::from("046a 454 286").is_valid());
}
use std::string::ToString;
pub struct Luhn {
code: String,
}
impl Luhn {
pub fn is_valid(&self) -> bool {
is_valid(&self.code)
}
}
/// Here is the example of how the From trait could be implemented
/// for the &str type. Naturally, you can implement this trait
/// by hand for the every other type presented in the test suite,
/// but your solution will fail if a new type is presented.
/// Perhaps there exists a better solution for this problem?
impl<T: ToString> From<T> for Luhn {
fn from(v: T) -> Luhn {
Luhn {
code: v.to_string(),
}
}
}
fn capped_double(d: u32) -> u32 {
if d >= 5 {
d * 2 - 9
} else {
d * 2
}
}
fn is_valid(code: &str) -> bool {
let res = code
.chars()
.filter(|c| !c.is_whitespace())
.map(|c| c.to_digit(10))
.rev()
.enumerate()
.try_fold((0, 0), |(sum, count), (i, opt)| {
if let Some(d) = opt {
if i & 1 == 1 {
Some((sum + capped_double(d), count + 1))
} else {
Some((sum + d, count + 1))
}
} else {
None
}
});
if let Some((sum, count)) = res {
sum % 10 == 0 && count > 1
} else {
false
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments