我遵循Exercism.io上的Rust轨道。我有大量的C / C ++经验。我喜欢Rust的“功能性”元素,但我担心相对性能。
我解决了“游程编码”问题:
pub fn encode(source: &str) -> String {
let mut retval = String::new();
let firstchar = source.chars().next();
let mut currentchar = match firstchar {
Some(x) => x,
None => return retval,
};
let mut currentcharcount: u32 = 0;
for c in source.chars() {
if c == currentchar {
currentcharcount += 1;
} else {
if currentcharcount > 1 {
retval.push_str(¤tcharcount.to_string());
}
retval.push(currentchar);
currentchar = c;
currentcharcount = 1;
}
}
if currentcharcount …Run Code Online (Sandbox Code Playgroud)