rust_persian_tools/commas/
remove_commas.rs1pub fn remove_commas(str: impl AsRef<str>) -> String {
14 str.as_ref().replace(',', "")
15}
16
17pub fn remove_commas_mut(str: &mut String) {
29 str.retain(|c| c != ',')
30}
31
32use std::borrow::Cow;
33pub trait RemoveCommas {
34 fn remove_commas(&self) -> String;
35}
36
37impl RemoveCommas for String {
38 fn remove_commas(&self) -> String {
39 remove_commas(self)
40 }
41}
42
43impl RemoveCommas for str {
44 fn remove_commas(&self) -> String {
45 remove_commas(self)
46 }
47}
48
49impl RemoveCommas for Cow<'_, str> {
50 fn remove_commas(&self) -> String {
51 remove_commas(self)
52 }
53}
54
55pub trait RemoveCommasMut {
56 fn remove_commas_mut(&mut self);
57}
58
59impl RemoveCommasMut for String {
60 fn remove_commas_mut(&mut self) {
61 remove_commas_mut(self)
62 }
63}
64impl RemoveCommasMut for Cow<'_, str> {
65 fn remove_commas_mut(&mut self) {
66 remove_commas_mut(self.to_mut())
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn remove_commas_test() {
76 assert_eq!(remove_commas("30,000,000"), "30000000".to_string());
77 }
78
79 #[test]
80 fn remove_commas_mut_test() {
81 let mut input = String::from("30,000,000");
82 remove_commas_mut(&mut input);
83 assert_eq!(input, "30000000".to_string());
84 }
85}