Created
December 3, 2019 00:06
-
-
Save rust-play/78a0c73866b922dfd490c11ab46d66a7 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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