Loading Rust to TOML Converter...
How to Convert a Rust Struct to TOML
Default settings often live in Rust code as a big struct literal, while the app reads its config from a TOML file. Paste the struct here and you get the matching file, without writing a quick program around toml::to_string first.
Paste a Rust value
Paste a struct literal such as Config { port: 8080, name: "app".to_string() }, or a whole let statement. You can also upload a .rs file or click Sample. A file that also holds struct definitions and a fn main works too: the value of the first let statement is used.
let config = Config {
name: "billing-api".to_string(),
tenant_id: 8901240544102066246,
fee_rate: 14.50,
zip: "00501".to_string(),
replica: None,
server: Server { host: "0.0.0.0".to_string(), port: 8080 },
jobs: vec![
Job { name: "invoices".to_string(), enabled: true },
Job { name: "cleanup".to_string(), enabled: false },
],
};Read the TOML
The TOML shows up on the right a moment after you stop typing. Plain fields come first, then one [table] per nested struct and one [[table]] entry per item of a Vec of structs. If something is not plain data, like a variable or a macro call, the error names the line and column.
The example above comes out like this. The None field is gone, the ID and the zero in 14.50 are untouched, and each job gets its own [[jobs]] block:
name = "billing-api" tenant_id = 8901240544102066246 fee_rate = 14.50 zip = "00501" [server] host = "0.0.0.0" port = 8080 [[jobs]] name = "invoices" enabled = true [[jobs]] name = "cleanup" enabled = false
Copy or download it
Copy puts the TOML on your clipboard, Download saves it as config.toml in UTF-8. Both give you exactly what is on the screen.
What is Rust to TOML Conversion?
TOML is the config format Rust people meet first, through Cargo.toml, and many Rust programs read their own settings from a TOML file with the toml crate and Serde. A struct maps onto it directly: each field is a key, each nested struct is a table.
This page writes that file from a value you already have, for example to ship a sample config or to check what a default struct looks like on disk. To check a TOML file, use the TOML Validator; to go the other way, try TOML to Rust, which writes the structs for a TOML file. For a Cargo manifest there is a separate Cargo.toml generator. Need another format? See Rust to JSON and Rust to YAML.
Frequently Asked Questions
What Rust input can I paste?
Values written in Rust syntax: a struct literal, a let or const statement, nested structs, vec![...] and arrays, tuples, HashMap::from([...]) and BTreeMap::from([...]), Some(...) and None, unit enum values like Level::Debug, and numeric constants such as u64::MAX or f64::INFINITY. Strings can use any Rust form (escapes like \n and \u{1F600}, raw strings, chars), and .to_string(), .into() and String::from(...) are understood. A struct definition on its own only lists field types, so there is nothing to write; paste a value instead.
How do nested structs, vectors and maps come out?
A nested struct becomes its own table, so server: Server { port: 8080 } is written under a [server] header. A Vec of structs becomes an array of tables, one [[jobs]] block per item. A Vec of plain values, a tuple or an array stays on one line as [1, 2, 3]. A HashMap becomes a table whose keys are the map keys. A unit enum value is written as its variant name, the way Serde does it. Keys that are not plain words, like first name or a.b, are put in quotes so they stay one key.
Will my numbers change?
No. Numbers are copied as text and never pass through a JavaScript number, so 8901240544102066246 and -9223372036854775808 keep every digit and 14.50 keeps its trailing zero. Underscores and type suffixes are dropped, hex and binary literals are written as decimal, and 5f64 becomes 5.0 because it is a float. TOML integers stop at the i64 range, so a u64 above 9223372036854775807 or an i128 is written as a quoted string with all its digits, and a note says so.
What happens to None and Option fields?
TOML has no null. A field set to None is left out of the file, which is also what the toml crate does when it serializes an Option field, and a note lists what was dropped. Some(value) is written as the value itself. A None inside a list is dropped too; the toml crate would refuse that list, so it is worth checking.
Is this the same as toml::to_string with Serde?
For ordinary config structs the layout matches: plain keys, then tables, then arrays of tables. There are a few deliberate differences. The crate prints an f64 like 14.50 as 14.5, while this page keeps what you typed. The crate returns an error for a u64 that does not fit in an i64, while this page writes it as a string. A byte string like b"hi" is written as text here, where Serde would write a list of byte values.
Is my code uploaded anywhere?
No. The conversion runs in your browser and the code is not sent to our server. Nothing is kept either: a reload starts with an empty editor. Uploaded files are read in the browser too, up to 10 MB.
Related Tools
Rust to JSON
Convert Rust structs and data structures to JSON format
Rust to YAML
Convert Rust structs to YAML configuration format
Rust to XML
Convert Rust structs to XML format
Rust to CSV
Export Rust data to CSV for spreadsheets and data analysis
Rust to SQL
Generate SQL CREATE TABLE and INSERT statements from Rust structs
Rust to Protobuf
Generate Protocol Buffers schema from Rust structs for gRPC