Crates.io | chemical-formula |
lib.rs | chemical-formula |
version | 0.1.0 |
source | src |
created_at | 2024-02-22 21:14:51.130752 |
updated_at | 2024-02-22 21:14:51.130752 |
description | Chemical formula parser that can handle wt% and nested structure |
homepage | https://github.com/Ameyanagi/chemical-formula-rs/tree/v0.1.0 |
repository | https://github.com/Ameyanagi/chemical-formula-rs |
max_upload_size | |
id | 1149727 |
size | 76,758 |
This crate provides a simple way to parse and manipulate chemical formulas including weight percent and nested formulas.
The initial motivation was to parse a formula such as Pt5wt%/SiO2
, which are heavily used annotation in the field of Heterogeneous catalysis.
Another motivatation was to parse a formula that is nested such as (Pt5wt%/SiO2)50wt%(CeO2)50wt%
, which can be used to describe a composite material.
We also provide a way simple API to convert between molecular formula and weight percent.
Please use cargo-edit to always add the latest version of this library:
cargo add chemical-formula
use chemical_formula::prelude::*;
fn main() {
let formula = parse_formula("H2O").unwrap();
println!("Orignal formula: {:?}", formula);
// Orignal formula: ChemicalFormula { element: {O, H}, stoichiometry: {H: 2.0, O: 1.0}, wt_percent: {} }
println!("Molecular weight: {:?}", formula.molecular_weight());
// Molecular weight: Ok(18.015)
println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
// Wt%: ChemicalFormula { element: {O, H}, stoichiometry: {}, wt_percent: {H: 11.19067443796836, O: 88.80932556203165} }
let formula = parse_formula("Pt5wt%/SiO2").unwrap();
println!("Orignal formula: {:?}", formula);
// Orignal formula: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {Si: 1.0, O: 2.0}, wt_percent: {Pt: 5.0} }
println!(
"Molecular Formula: {:?}",
formula.to_molecular_formula().unwrap()
);
// Molecular Formula: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {Si: 1.0, Pt: 0.016209751480873558, O: 2.0}, wt_percent: {} }
println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
// Wt%: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {}, wt_percent: {Pt: 5.0, Si: 44.406487692026026, O: 50.59351230797397} }
let formula = parse_formula("(Pt5wt%/SiO2)50wt%(CeO2)50wt%").unwrap();
println!("Orignal formula: {:?}", formula);
// Orignal formula: ChemicalFormula { element: {Si, Ce, Pt, O}, stoichiometry: {}, wt_percent: {Si: 22.203243846013017, O: 34.59233931398559, Pt: 2.5000000000000004, Ce: 40.70441684000139} }
println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
// Wt%: ChemicalFormula { element: {Si, Ce, Pt, O}, stoichiometry: {}, wt_percent: {Si: 22.203243846013017, O: 34.59233931398559, Pt: 2.5000000000000004, Ce: 40.70441684000139} }
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
See CONTRIBUTING for more information.
chemical-formula-rs
strictly follows Semantic Versioning 2.0.0 with the following exceptions:
This includes the Rust version requirement specified above.
Earlier Rust versions may be compatible, but this can change with minor or patch releases.
Which versions are affected by features and patches can be determined from the respective headings in CHANGELOG.md.