Loading TOML to Rust Converter...
Please wait a moment

How to Convert TOML to Rust - Step by Step Guide

Step 1

Input Your TOML Configuration

Start by providing your TOML configuration file for Rust struct generation. Whether you're working with Cargo.toml, application configs, or configuration files for your Rust application, you have several convenient options. Use AI assistants like Claude or Grok to further optimize the generated code:

•
Paste directly: Copy your TOML from Cargo.toml, config.toml, or any TOML configuration file
•
Upload a file: Click "Upload" to select a .toml file from your computer
•
Try the sample: Click "Sample" to load example TOML and see how the converter works

Example: TOML Configuration Input

A typical TOML configuration file with nested tables:

[server]
host = "localhost"
port = 8080
ssl_enabled = true

[database]
name = "myapp_db"
max_connections = 100
hosts = ["db1.com", "db2.com"]
Step 2

Automatic Rust Code Generation

The converter instantly generates type-safe Rust structs with proper serde annotations:

•
Type inference: Automatically detects types from TOML values (strings, numbers, booleans, arrays, tables)
•
Nested tables: Creates separate structs for nested TOML tables with proper relationships
•
Serde annotations: Adds #[derive(Serialize, Deserialize)] for TOML serialization

Example: Generated Rust Structs

The same TOML converted to idiomatic Rust code:

use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
    pub server: Server,
    pub database: Database,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Server {
    pub host: String,
    pub port: i64,
    pub ssl_enabled: bool,
}
Step 3

Customize Generation Options

Tailor the generated Rust code to match your project's conventions and requirements:

•
Struct name: Customize the root struct name to match your configuration model
•
Derive traits: Add additional derives like Clone, PartialEq, Default
•
Field visibility: Make the structs and fields pub, or leave them private to the module
Step 4

Export Your Rust Code

Get your generated Rust structs ready for your project! Multiple export options make integration seamless:

•
Copy to clipboard: One-click copying to paste directly into your .rs files
•
Download as file: Save as a .rs file ready to add to your Rust project
•
Ready-to-compile: Generated code includes all necessary imports and annotations

Frequently Asked Questions

What is TOML and why is it popular in Rust?

TOML (Tom's Obvious Minimal Language) is a simple, human-readable configuration file format. It's the standard format for Cargo.toml in Rust projects and is widely used for application configurations. Converting TOML to Rust structs ensures type-safe configuration loading with compile-time validation.

Can I use this to parse my Cargo.toml?

Yes. Paste the whole file or just the sections you care about, such as a custom [package.metadata] table. Each table becomes a struct, and you load the file with toml::from_str. Keep in mind Cargo allows several shapes for the same key (a dependency can be "1.0" or { version = "1.0" }), and the structs only cover the shape in your file.

What dependencies do I need to use the generated code?

Two crates: serde with the derive feature, and toml for reading the file (dates and mixed arrays also use types from the toml crate). Add to your Cargo.toml: serde = { version = "1.0", features = ["derive"] } and toml = "0.8".

How does the converter handle TOML tables and arrays?

Every table, like [server] or [a.b.c], gets its own struct. Arrays become Vec<T>, and every item is looked at, not just the first: if some [[products]] entries have a color and others don't, color becomes Option<String>, and a list holding 1 and 2.5 becomes Vec<f64>. Whole numbers are i64 (long IDs like 8901240544102066246 fit), 1.0 counts as a float, and dates become toml::value::Datetime. Empty arrays and arrays mixing types fall back to Vec<toml::Value>.

Can I convert the generated structs back to TOML?

Yes, as long as Serialize stays in the derive list: toml::to_string() writes the structs back out. Comments and the original formatting don't survive the trip, and fields that are None are left out.

Is the converter free to use?

Yes, and there's no sign-up. The conversion happens in your browser, so your config never leaves your machine. Uploaded files can be up to 10 MB; very large files may take a moment to convert.