Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update luhn.rs #1486

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Update luhn.rs #1486

wants to merge 1 commit into from

Conversation

aadeexyz
Copy link

Made it easier to read and also got rid of unwrap

Made it easier to read and also got rid of unwrap
Comment on lines +19 to +21
let cc_number = cc_number.replace(" ", "");
let mut sum = 0;
let mut double = false;
let mut digit_seen = 0;
let mut even = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you're essentially doing here is trading an allocation (cc_number.replace(" ", "") will typically have to allocate) for the digit_seen variable which tracks the number of characters seen.

I would prefer to avoid the allocation and thus remove the call to replace — allocation is not necessary when we can just filter out the spaces.

Your PR is useful since it can refactor the nested matching and if-statements into something that uses an early return.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you take out this part since the allocation is ultimately unnecessary?

num = num * 2;

if num > 9 {
num = (num / 10) + (num % 10);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this code in the past 🙂 See the history for this exercise.

People said in the past that the if sum > 9 { sum = sum - 9 } logic is easier to understand.

double = !double;
digit_seen += 1;
} else {
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to turn this return into an early return.

Comment on lines +30 to +33
match c.to_digit(10) {
Some(n) => num = n,
None => return false,
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the if let Some(num) = c.to_digit(10) else return false pattern here.

Copy link
Collaborator

@mgeisler mgeisler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @AadeeWasTaken, could you merge in the latest main and see if there are still things to improve here?

Please also take out the parts that I commented on. Thanks!

Comment on lines +19 to +21
let cc_number = cc_number.replace(" ", "");
let mut sum = 0;
let mut double = false;
let mut digit_seen = 0;
let mut even = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you take out this part since the allocation is ultimately unnecessary?

@randomPoison randomPoison added the waiting for author Waiting on the issue author to reply label Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for author Waiting on the issue author to reply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants