Hello,
I have to replace german umlauts in a file name with ae, ue, oe etc.. But I can't think of an effective solution. In other languages, for example D, I'm used to a translate method that accepts a hash map and replaces the keys with the respective values. However, I have not yet discovered something similar in Rust.
Here is my current solution, but I think that it could be improved:
use std::collections::HashMap;
trait Transcode {
fn encode(&self) -> String;
}
impl Transcode for str {
fn encode(&self) -> String {
let mut trans = HashMap::new();
trans.insert('ä', 'a');
trans.insert('ü', 'u');
trans.insert('ö', 'o');
trans.insert('Ä', 'A');
trans.insert('Ü', 'U');
trans.insert('Ö', 'O');
let mut cs = Vec::new();
for c in self.chars() {
if let Some(t) = trans.get(&c) {
cs.push(*t);
cs.push('e');
} else {
cs.push(c);
}
}
let s: String = cs.into_iter().collect();
return s;
}
}