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

How to Turn a .proto File into Rust Structs

The usual way to get Protocol Buffers types in Rust is prost-build in a build script. When you just want to see what a schema turns into, try a message type in a playground, or paste a few structs into a crate without a build step, this page writes the same kind of prost structs straight from the .proto text.

Step 1

Paste or upload the .proto file

Paste a schema into the left editor, upload a .proto file (up to 10 MB), or press Sample. proto2, proto3 and edition files work, and a snippet without a syntax line is read as proto3.

Example: a small order schema

syntax = "proto3";

package shop.v1;

message Order {
  int64 id = 1;
  string customer_id = 2;
  repeated Item items = 3;
  optional string coupon = 4;
  map<string, string> labels = 5;
}

message Item {
  string sku = 1;
  uint32 qty = 2;
  double price = 3;
}
Step 2

Read the Rust types

Each message becomes a struct with #[prost(...)] field attributes, each enum a #[repr(i32)] enum, and each oneof an enum of its own. A short list above the code tells you about anything that needs a look, such as a service that was left out or a type from a file you didn't paste. A mistake in the .proto file shows its line and column instead.

What the page writes for that schema

The id stays an i64, the optional coupon is an Option<String>, and customer_id keeps the JSON name customerId through a serde rename.

// Rust types for this .proto file, written for prost (binary Protobuf) and serde (JSON).
// Cargo.toml:
//   prost = "0.14"
//   serde = { version = "1", features = ["derive"] }
//   serde_json = "1"
// Read binary data with  use prost::Message;  then  Type::decode(bytes)
// .proto package: shop.v1
//
// Note: 64-bit fields are i64/u64, so long IDs such as 8901240544102066246 keep every digit. serde_json reads and writes them as plain JSON numbers; protobuf's own JSON format puts them in quotes.

use std::collections::HashMap;

/// Message `shop.v1.Order`.
#[derive(Clone, PartialEq, ::prost::Message, ::serde::Serialize, ::serde::Deserialize)]
#[serde(default)]
pub struct Order {
    #[prost(int64, tag = "1")]
    pub id: i64,
    #[prost(string, tag = "2")]
    #[serde(rename = "customerId")]
    pub customer_id: String,
    #[prost(message, repeated, tag = "3")]
    pub items: Vec<Item>,
    #[prost(string, optional, tag = "4")]
    pub coupon: Option<String>,
    #[prost(map = "string, string", tag = "5")]
    pub labels: HashMap<String, String>,
}

/// Message `shop.v1.Item`.
#[derive(Clone, PartialEq, ::prost::Message, ::serde::Serialize, ::serde::Deserialize)]
#[serde(default)]
pub struct Item {
    #[prost(string, tag = "1")]
    pub sku: String,
    #[prost(uint32, tag = "2")]
    pub qty: u32,
    #[prost(double, tag = "3")]
    pub price: f64,
}
Step 3

Copy or download the code

Copy the code or download protobuf_types.rs, add the crates listed at the top of the file to Cargo.toml, and decode messages with prost.

use prost::Message;

let order = Order::decode(bytes.as_slice())?;
println!("{} {} {}", order.id, order.customer_id, order.items.len());

let json = serde_json::to_string(&order)?;

To check a schema before converting it, try the Proto File Validator. If you only have JSON data, JSON to Rust builds serde structs straight from a sample document, and the serde_json guide covers reading JSON in Rust in more depth.

Frequently Asked Questions

Is this the same code prost-build generates?

Close, but not identical. The field attributes, type mapping, boxing of recursive fields and enum variant names follow prost-build, so the structs encode and decode the same bytes. The differences: nested messages, enums and oneofs are written as top-level types (Order.LineItem becomes OrderLineItem) instead of inside a module, and serde derives are added to every message. For a full build pipeline with tonic services, use prost-build or tonic-build in build.rs.

How are .proto types mapped to Rust?

int32, sint32 and sfixed32 become i32; int64, sint64 and sfixed64 become i64; uint32 and fixed32 become u32; uint64 and fixed64 become u64. float is f32, double is f64, string is String and bytes is Vec<u8>. repeated fields are Vec<T>, map<K, V> fields are HashMap<K, V>, message fields are Option<T>, and enum fields are i32 tagged with the enum name, as prost does. Fields marked optional (and every optional field in proto2) are Option<T>; required proto2 fields are plain values.

Do long 64-bit IDs keep all their digits?

Yes. An int64 field is an i64 and a uint64 field is a u64, so values like 8901240544102066246 or 18446744073709551615 come through prost and serde_json without rounding. If you read JSON written by protobuf's own JSON printer, note that it puts 64-bit numbers in quotes, while the serde derives here expect plain numbers.

What does the JSON from the serde derives look like?

Field names use protobuf's JSON names (customer_id is written as customerId, and json_name is respected), missing fields fall back to their defaults, and the members of a oneof appear as ordinary keys. Enum fields are numbers rather than value names, and bytes are arrays of numbers rather than Base64, because the derives work on the same i32 and Vec<u8> fields prost uses. Types from the prost-types crate, such as Timestamp and Any, have no serde support, so those fields are skipped in JSON.

What happens to services, imports and default values?

Services are left out with a note, because client and server code comes from tonic-build. A message from an imported file that you did not paste is kept as its encoded bytes (Vec<u8>), which still reads and writes the field unchanged; paste the imported file too to get a real struct. Well-known types map to prost-types or to plain Rust types for the wrappers, and proto2 default values are copied into the prost attributes from the original text, so a default like -9223372036854775808 keeps every digit.

Is my .proto file uploaded anywhere?

No. The conversion runs in your browser and the file is not sent to our server. Nothing is kept either: when you reload the page, the editor starts empty.