Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created December 3, 2019 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/78a0c73866b922dfd490c11ab46d66a7 to your computer and use it in GitHub Desktop.
Save rust-play/78a0c73866b922dfd490c11ab46d66a7 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
let formula = String::from("K4(ON(Mg2O3)2(Rg)3)2");
let formula_vec: Vec<char> = formula.chars().collect();
let result = formula
.chars()
.enumerate()
.fold(String::new(), |mut acc, (i, c)| {
if i + 1 < formula_vec.len() && (!formula_vec[i + 1].is_ascii_digit() || !formula_vec[i + 1].is_ascii_lowercase()) {
acc = format!("{}{}{}", acc, c, "1");
} else {
acc = format!("{}{}", acc, c);
}
acc
});
println!("{:?}", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment