Skip to content

Commit

Permalink
Merge pull request #1 from MggMuggins/main
Browse files Browse the repository at this point in the history
Remove unessesary iteration
  • Loading branch information
grantshandy authored Mar 16, 2022
2 parents 9309d48 + c26cd69 commit c11f1ca
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
An IMEI number validator implemented in Rust.

Add to Cargo.toml:
```
```toml
imei = "1.0.0"
```

Expand All @@ -18,7 +18,7 @@ fn main() {
```

Result:
```
```toml
490154203237518: true
```

Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

/// Check to see if an IMEI number is valid.
pub fn valid<A: AsRef<str>>(imei: A) -> bool {
let chars = imei.as_ref().chars();
let s = imei.as_ref();

// check for number length
if chars.clone().count() != 15 {
// str::len is acceptable because if s is numeric (therefore valid),
// there will not be issues with UTF-8
if s.len() != 15 {
return false;
}

// the sum of calculated digits
let mut sum: u8 = 0;

// go through each character in the imei
for (i, c) in chars.enumerate() {
for (i, c) in s.chars().enumerate() {
// convert the chars into u8
// I precalculated these so it doesn't have to parse the char as a string
// It also makes sure that the input is only numeric
Expand Down

0 comments on commit c11f1ca

Please sign in to comment.