1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
pub trait RemoveDuplicateSpaces { fn remove_duplicate_spaces(self) -> Self; } pub trait Indent { fn indent(self, number: usize) -> Self; } impl RemoveDuplicateSpaces for String { fn remove_duplicate_spaces(self) -> Self { let mut replaced = self; while replaced.contains(" ") { replaced = replaced.replace(" ", " "); } replaced } } impl Indent for String { fn indent(self, number: usize) -> Self { let mut padding = "".to_string(); for _ in 0..number { padding = format!(" {}", padding); } let lines: Vec<String> = self.lines().map(|l| format!("{}{}", padding, l)).collect(); lines.join("\n") } }