-
-
Save rust-play/2c8312fd34c110474afa3c6d2266842f to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or 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
| // Cargo.toml dependencies: | |
| // serde = { version = "1.0", features = ["derive"] } | |
| // chrono = "0.4" | |
| // rand_0_8_5 = { package = "rand", version = "0.8.5" } | |
| use serde::Serialize; | |
| use std::collections::HashMap; | |
| /// Speed of light (c) in m/s | |
| const C: f64 = 299_792_458.0; | |
| #[derive(Debug, Serialize)] | |
| struct SpectralLine { | |
| wavelength_nm: f64, | |
| frequency_thz: f64, | |
| } | |
| impl SpectralLine { | |
| fn new(nm: f64) -> Self { | |
| // f = c / lambda | |
| let freq_hz = C / (nm * 1e-9); | |
| Self { | |
| wavelength_nm: nm, | |
| frequency_thz: freq_hz / 1e12, | |
| } | |
| } | |
| } | |
| fn main() { | |
| let mut periodic_table_lines: HashMap<&str, Vec<f64>> = HashMap::new(); | |
| // Mapping prominent persistent lines (nm) for various elements from the image | |
| periodic_table_lines.insert("Hydrogen", vec![656.3, 486.1, 434.1, 410.2]); // Balmer Series | |
| periodic_table_lines.insert("Helium", vec![587.6, 667.8, 501.6, 447.1]); | |
| periodic_table_lines.insert("Lithium", vec![670.8, 610.4, 460.3]); | |
| periodic_table_lines.insert("Mercury", vec![546.1, 435.8, 404.7, 579.1]); | |
| periodic_table_lines.insert("Neon", vec![585.2, 640.2, 614.3, 540.1, 703.2]); | |
| periodic_table_lines.insert("Sodium", vec![589.0, 589.6]); // The famous D-lines | |
| periodic_table_lines.insert("Titanium", vec![498.1, 499.1, 500.7, 365.3]); | |
| periodic_table_lines.insert("Gold", vec![479.2, 583.7, 627.8]); | |
| println!("{:<10} | {:<20} | {:<20}", "Element", "Wavelength (nm)", "Frequency (THz)"); | |
| println!("{:-<55}", ""); | |
| // Iterate through all stored elements | |
| for (element, lines) in &periodic_table_lines { | |
| for nm in lines { | |
| let line = SpectralLine::new(*nm); | |
| println!( | |
| "{:<10} | {:<20.2} | {:<20.2}", | |
| element, line.wavelength_nm, line.frequency_thz | |
| ); | |
| } | |
| println!("{:-<55}", ""); | |
| } | |
| // Logic Note: | |
| // To print ALL ~100,000 lines from the NIST database, you would typically | |
| // parse a CSV/JSON export from NIST ASD (Atomic Spectra Database). | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment