Skip to content

Commit e8fea47

Browse files
committed
DRY and readability
1 parent f66bdff commit e8fea47

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

rust/phone-number/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
use std::ops::Range;
2+
3+
const AREA_RANGE : Range<usize> = 0..3;
4+
const EXCHANGE_RANGE : Range<usize> = 3..6;
5+
const SUBSCRIBER_RANGE : Range<usize> = 6..10;
6+
17
fn sanitize(input: &str) -> String {
28
input.matches(char::is_numeric).collect()
39
}
@@ -15,12 +21,12 @@ pub fn number(input: &str) -> Option<String> {
1521
}
1622

1723
pub fn area_code(input: &str) -> Option<String> {
18-
number(input).map(|n| n[0..3].to_string())
24+
number(input).map(|n| n[AREA_RANGE].to_string())
1925
}
2026

2127
pub fn pretty_print(input: &str) -> String {
2228
match number(input) {
23-
Some(n) => format!("({}) {}-{}", &n[0..3], &n[3..6], &n[6..10]),
29+
Some(n) => format!("({}) {}-{}", &n[AREA_RANGE], &n[EXCHANGE_RANGE], &n[SUBSCRIBER_RANGE]),
2430
None => "invalid".to_string(),
2531
}
2632
}

0 commit comments

Comments
 (0)