TOML to Rust Struct Generator
Automatically generate Rust structs from your TOML configuration files. This tool creates production-ready Rust code with Serde derive attributes, making it easy to deserialize TOML into strongly-typed structs. A must-have for Rust developers working with config files.
How the TOML to Rust Struct Generator Works
This tool converts TOML configuration files into Rust struct definitions with Serde attributes. Paste your TOML content and get idiomatic Rust code ready for deserializing configuration in your Rust applications.
The converter maps TOML types to Rust types: strings to String, integers to i64 or u64, floats to f64, booleans to bool. Tables become nested structs. Arrays become Vec<T>. Serde derive attributes enable seamless deserialization.
Generated code includes #[derive(Serialize, Deserialize)] and #[serde(rename = "...")] attributes for proper TOML field mapping. Copy into your Rust project, add the toml and serde crates, and deserialize configs with toml::from_str().
When You'd Actually Use This
Building Rust applications with config
Your Rust app needs configuration. Define settings in TOML (Rust's preferred config format), generate structs, deserialize with toml crate. Type-safe configuration.
Creating Cargo workspace configs
Cargo uses TOML for Cargo.toml. Generate Rust structs to parse custom workspace configuration. Build tools that read and validate Cargo configurations.
Building CLI tools in Rust
Rust CLI tools often use TOML configs. Generate structs for your tool's configuration. Combine with clap for CLI args that override config file settings.
Validating configuration at compile time
Rust's type system catches configuration errors. Missing fields, wrong types, all caught by the compiler. Much safer than runtime config parsing.
Creating game engine configurations
Game engines need extensive configuration. Define in TOML, generate Rust structs for graphics, audio, input settings. Hot-reload configs during development.
Learning Rust serialization
See how TOML maps to Rust types. Study generated Serde attributes to understand Rust serialization patterns. Great learning resource for Rust beginners.
What to Know Before Using
Rust has strong type requirements.TOML integers become i64 by default. Specify u64, i32, etc. manually if needed. Rust won't silently convert between numeric types.
Serde attributes enable deserialization.#[serde(rename = "toml_key")] maps Rust snake_case fields to TOML keys. Don't remove these or deserialization will fail.
Option<T> for optional fields.Use Option<String>, Option<i64> for fields that might be missing. None means not specified in TOML. Some(value) means present.
Nested tables become nested structs.Each TOML table level generates a separate struct. Structs reference each other for the full configuration hierarchy.
Pro tip: Add #[serde(default)] to structs for default values on missing fields. Combine with Option for flexible configuration that doesn't require all fields.
Common Questions
How do I deserialize TOML in Rust?
Use toml::from_str::<Config>(toml_string). Returns Result<Config, toml::de::Error>. Handle the Result to get your config or report errors.
What crates do I need?
Add to Cargo.toml: serde = { version = "1", features = ["derive"] }, toml = "0.8". These provide serialization traits and TOML parsing.
How do I handle errors?
toml::from_str returns a Result. Use match, if let, or ? operator. Display errors with .to_string() or use error handling crates like thiserror.
Can I add custom validation?
Yes, implement a validate() method on your struct. Check ranges, relationships between fields, etc. Call after deserialization.
What about datetime types?
TOML datetimes map to toml::datetime::Datetime or chrono::DateTime. Add chrono crate with serde feature for better datetime handling.
How do I load from a file?
Read file with std::fs::read_to_string(), then parse: let config: Config = toml::from_str(&content)?;. Handle both IO and parse errors.
Can I use enums in config?
Yes, define enums with Serde. TOML strings map to enum variants. Use #[serde(rename_all = "lowercase")] for clean TOML syntax.
Other Free Tools
TOML to JSON Converter
Convert TOML to JSON Instantly
JSON to TOML Converter
Convert JSON to TOML Online
TOML Validator and Linter
Validate and Lint Your TOML Files
TOML Beautifier and Formatter
Beautify and Format TOML Code
TOML to YAML Converter
Convert TOML to YAML Easily
ASCII to Hex Converter
ASCII to Hex Converter: Text to Hexadecimal Translator
Barcode Generator
Free Barcode Generator
Binary to Text Converter
Binary to Text Converter
Free Printable Calendar Maker
Create & Print Your Custom Calendar
Pie Chart Maker
Free Pie Chart Maker Online