print_all_elements/
print_all_elements.rs

1use mendeleev::Element;
2
3/// Prints all the elements and their properties to stdout, as a tab-separated table
4fn main() {
5    let columns: Vec<(&str, Box<dyn Fn(&Element) -> String>)> = vec![
6        (
7            "Number",
8            Box::new(|e: &Element| e.atomic_number().to_string()),
9        ),
10        ("Symbol", Box::new(|e: &Element| e.symbol().to_string())),
11        (
12            "Name      ",
13            Box::new(|e: &Element| format!("{:<10}", e.name())),
14        ),
15        (
16            "Year",
17            Box::new(|e: &Element| e.year_discovered().to_string()),
18        ),
19        (
20            "CPK color",
21            Box::new(|e: &Element| {
22                format!(
23                    "{:<10}",
24                    e.cpk_color()
25                        .map(|c| c.to_string())
26                        .unwrap_or("".to_string()),
27                )
28            }),
29        ),
30        (
31            "Jmol color",
32            Box::new(|e: &Element| {
33                format!(
34                    "{:<10}",
35                    e.jmol_color()
36                        .map(|c| c.to_string())
37                        .unwrap_or("".to_string()),
38                )
39            }),
40        ),
41        (
42            "Atomic Weight",
43            Box::new(|e: &Element| format!("{:<10}", e.atomic_weight().to_string())),
44        ),
45        (
46            "Atomic Radius",
47            Box::new(|e: &Element| {
48                format!(
49                    "{:10}",
50                    e.atomic_radius()
51                        .map(|r| r.to_string())
52                        .unwrap_or("".to_string())
53                )
54            }),
55        ),
56        ("Period", Box::new(|e: &Element| e.period().to_string())),
57        (
58            "Group",
59            Box::new(|e: &Element| {
60                format!("{:<10}", e.group().map(|g| g.group_symbol()).unwrap_or(""))
61            }),
62        ),
63    ];
64    println!(
65        "{}",
66        columns
67            .iter()
68            .map(|(name, _)| *name)
69            .collect::<Vec<_>>()
70            .join("\t")
71    );
72    for element in Element::list() {
73        println!(
74            "{}",
75            columns
76                .iter()
77                .map(|(_, prop)| prop(&element))
78                .collect::<Vec<_>>()
79                .join("\t")
80        );
81    }
82}