Loading Rust to SQL Converter...

How to Convert Rust Structs to SQL

You already described your data once, as a Rust struct. Paste it here and you get the matching table, plus INSERT statements for any test data you have sitting in a vec!, so you can seed a local database without typing the rows out again.

Step 1

Paste your structs, your data, or both

A struct definition gives the table and its column types. Struct values, such as a vec![...] of User { ... } or a let users = vec![...]; statement, become INSERT rows. You can paste a whole file: struct definitions are read, impl blocks are skipped, and the let statements inside fn bodies that hold struct values are picked up. Upload a .rs file or click Sample if you just want to see how it works.

struct Sim {
    iccid: i64,
    zip: String,
    price: f64,
    owner: Option<String>,
    active: bool,
}

vec![
    Sim { iccid: 8901240544102066246, zip: "00501".to_string(), price: 14.50, owner: Some("O'Brien".to_string()), active: true },
    Sim { iccid: 8901240544102066247, zip: "10115".to_string(), price: 9.99, owner: None, active: false },
]
Step 2

Read the SQL

The SQL shows up on the right a moment after you stop typing. Each struct becomes a table named in snake_case (UserProfile turns into user_profile), each field becomes a column, and every value is checked against its field type. If a value does not fit, for example 300 in a u8 field or None in a field that is not an Option, the error names the line instead of writing SQL that would fail later.

The example above comes out like this. The ICCIDs keep all 19 digits, the ZIP code keeps its leading zeros, the apostrophe in O'Brien is doubled and the None becomes NULL:

-- struct Sim
CREATE TABLE sim (
  iccid BIGINT NOT NULL,
  zip TEXT NOT NULL,
  price DOUBLE PRECISION NOT NULL,
  owner TEXT,
  active BOOLEAN NOT NULL
);

INSERT INTO sim (iccid, zip, price, owner, active) VALUES (8901240544102066246, '00501', 14.50, 'O''Brien', TRUE);
INSERT INTO sim (iccid, zip, price, owner, active) VALUES (8901240544102066247, '10115', 9.99, NULL, FALSE);
Step 3

Copy it or save schema.sql

Copy puts the SQL on your clipboard and Download saves it as schema.sql. The blue notes above the output tell you about anything worth knowing before you run it, such as columns stored as JSON text or names that had to be quoted.

Where This Fits in a Rust Project

Crates like SQLx and Diesel map rows to structs, but you still write the table by hand. This page writes a first version of that table from the struct, which saves the typing and the type lookups. Add your primary key, indexes and defaults, then run it in a migration.

Have the data in another shape? Try JSON to SQL or CSV to SQL, or turn the same Rust values into a spreadsheet with Rust to CSV.

Frequently Asked Questions

How are Rust types mapped to SQL column types?

i8, i16 and u8 become SMALLINT, i32 and u16 become INTEGER, i64, isize and u32 become BIGINT. u64 and usize become NUMERIC(20) and i128 or u128 NUMERIC(39), because BIGINT stops at 9223372036854775807. f32 is REAL, f64 is DOUBLE PRECISION, bool is BOOLEAN and String, &str and char are TEXT. A field is NOT NULL unless its type is an Option. Vec, HashMap, tuples and arrays are stored as JSON text in a TEXT column, plain enums as the variant name, and dates, Uuid and Decimal as TEXT, so you can change those columns to DATE, UUID or NUMERIC if your database has them.

Do I need the struct definition, or are values enough?

Either works. With the definition, the column types come from the field types and every value is checked against them. With values only, the types are guessed from what is there: whole numbers become BIGINT (or NUMERIC when they are too long for it), decimals DOUBLE PRECISION, true and false BOOLEAN, and text TEXT, and every column is nullable because a missing field cannot be told apart from an optional one. A struct definition on its own gives you just the CREATE TABLE.

Will long IDs and decimals keep their digits?

Yes. 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 in the INSERT. Underscores and type suffixes are dropped (1_000u32 becomes 1000) and hex literals are written in decimal. Text such as "00501" stays a quoted string with its leading zeros.

Which databases can run the output?

The SQL is plain standard SQL and has been run in PostgreSQL 16 and SQLite. Strings only double their single quotes, so a value like O'Brien or even x'); DROP TABLE users; -- stays text. MySQL needs two settings for the same result: ANSI_QUOTES when a table or column name is quoted (user and order are SQL keywords, so a struct called User becomes "user"), and NO_BACKSLASH_ESCAPES when your text contains backslashes. The notes above the output say when either applies.

How are nested structs handled?

When a field holds another struct that you pasted too, its fields are flattened into the same table with the field name in front, so customer: Customer { city, zip } gives customer_city and customer_zip columns. If the outer field is an Option, those columns are nullable. A struct that only appears inside another one does not get a table of its own. There are no foreign keys or separate child tables; nested values in Vec fields go into a JSON text column.

Can I use the CREATE TABLE in sqlx or Diesel migrations?

It is a good starting point. Column names are the Rust field names, which is what sqlx FromRow and Diesel expect by default. There is no primary key, index or default value, because a struct does not say which field should be one, so add those before you check the migration in. Attributes like #[sqlx(rename)] or #[serde(rename)] are not read.

Is my code uploaded anywhere?

No. The conversion runs in your browser, your code is not sent to our server, and nothing is kept: a reload starts with an empty editor. Uploaded .rs files are read in the browser too, up to 10 MB.