Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rust TOML reading table and key-value in order

How to read TOML in order?

I have this code:

  let toml_content: Value = contents.parse::<toml::Value>().unwrap();
  for key in toml_content.as_table().unwrap().keys() {
    println!("{:?}", key);
  }

Returning output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"Community"
"Person"
"Wallet"

While I expect ordered output:

"Person"
"Wallet"
"Community"

Because I have this toml file, where Person is 1st:

[Person]
name = "str"
age = "u8"
ttl = "u8"
wallet = "Wallet"

[Wallet]
owner = "Person"
balance = "f64"

[Community]
name = "str"
members = ["Person"]

>Solution :

Assuming you’re using the toml crate, enable the preserve_order feature. This is documented here.

Add this into Cargo.toml

[dependencies]
toml = { version = "*", features = ["preserve_order"] }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading