MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / reformat_number

Function reformat_number

1694-reformat-phone-number.rs:1–21  ·  view source on GitHub ↗
(number: String)

Source from the content-addressed store, hash-verified

1pub fn reformat_number(number: String) -> String {
2 let tmp = number.replace("-", "").replace(" ", "");
3 let mut tmp_arr: Vec<_> = vec!();
4 for i in (0..tmp.len()).step_by(3) {
5 let mut tail = i+3;
6 if tail > tmp.len() {
7 tail = tmp.len();
8 }
9 tmp_arr.push(&tmp[i..tail]);
10 }
11 let mut tmp_s = String::new();
12 if tmp_arr.len() >= 2 && tmp_arr[tmp_arr.len()-1].len() == 1 {
13 tmp_s.push_str(tmp_arr[tmp_arr.len()-2]);
14 tmp_s.push_str(tmp_arr[tmp_arr.len()-1]);
15 tmp_arr.pop();
16 tmp_arr.pop();
17 tmp_arr.push(&tmp_s[0..2]);
18 tmp_arr.push(&tmp_s[2..4]);
19 }
20 tmp_arr.join("-")
21}
22
23fn main() {
24 let number = "9964-".to_string();

Callers

nothing calls this directly

Calls 2

pushMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected