Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
.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);
println!("{:?}", result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.