Skip to content

Commit 3534467

Browse files
authored
refactor(solutions/reverse_it): more concise and idiomatic code
1 parent 7418799 commit 3534467

File tree

1 file changed

+3
-6
lines changed
  • solutions/reverse_it/src

1 file changed

+3
-6
lines changed

solutions/reverse_it/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
pub fn reverse_it(nbr: i32) -> String {
22
let s = nbr.to_string();
3-
let str_nbr = s.trim_start_matches('-');
4-
let rev: String = str_nbr.chars().rev().collect();
5-
if nbr >= 0 {
6-
return format!("{}{}", rev, str_nbr);
7-
}
8-
return format!("-{}{}", rev, str_nbr);
3+
let abs_s = s.trim_start_matches('-');
4+
let rev = abs_s.chars().rev().collect::<String>();
5+
format!("{}{}{}", if nbr < 0 { "-" } else { "" }, rev, abs_s)
96
}
107

118
#[cfg(test)]

0 commit comments

Comments
 (0)