Add the toml crate to your Cargo.toml dependencies and use toml::from_str to parse TOML strings into Rust structs.
[dependencies]
toml = "0.8"
use serde::Deserialize;
#[derive(Deserialize)]
struct Config {
title: String,
edition: String,
}
fn main() {
let toml_str = r#"
title = "The Rust Programming Language"
edition = "2024"
"#;
let config: Config = toml::from_str(toml_str).unwrap();
println!("{}", config.title);
}
Note: You must also add serde = { version = "1.0", features = ["derive"] } to your dependencies to enable the Deserialize derive macro.