Prost for Rust: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Prost and why is it needed?

\n\n

Prost is a powerful and performant library for the Rust programming language, designed for working with Protocol Buffers (protobuf). Protocol Buffers is a structured data serialization mechanism developed by Google that is more compact and faster compared to JSON or XML.

\n\n

Prost solves several key tasks:

\n
    \n
  • Code generation: Automatically creates Rust structures from .proto files, saving the developer from routine work.
  • \n
  • Serialization and deserialization: Converts data from protobuf binary format to Rust structures and back.
  • \n
  • High performance: Uses zero-copy and efficient memory management, making it one of the fastest solutions for working with protobuf in Rust.
  • \n
  • Safety: Fully written in Rust, guaranteeing memory and thread safety.
  • \n
\n\n

The library is especially useful in microservice architecture, inter-service data exchange systems, configuration storage, and any scenarios requiring efficient serialization.

\n\n

Installation

\n\n

Prost is installed via the Cargo package manager. To get started, add the following dependencies to your Cargo.toml:

\n\n
[dependencies]\nprost = "0.12"\nprost-build = "0.12"\ntonic = "0.11"  # optional, for gRPC\nbytes = "1.5"   # for working with buffers\n\n[build-dependencies]\nprost-build = "0.12"
\n\n

You will also need to install the protobuf compiler (protoc). On Ubuntu/Debian:

\n
sudo apt install protobuf-compiler
\n\n

On macOS:

\n
brew install protobuf
\n\n

On Windows, download the binary from the official protobuf repository and add it to PATH.

\n\n

Quick Start

\n\n

Create a file person.proto:

\n\n
syntax = "proto3";\n\nmessage Person {\n    string name = 1;\n    int32 age = 2;\n    repeated string phone_numbers = 3;\n}
\n\n

Create build.rs in the project root:

\n\n
fn main() -> Result<(), Box<dyn std::error::Error>> {\n    prost_build::compile_protos(&["src/person.proto"], &["src/"])?;\n    Ok(())\n}
\n\n

Now in main.rs:

\n\n
use prost::Message;\n\n// Generated code will be in the person module\npub mod person {\n    include!(concat!(env!("OUT_DIR"), "/person.rs"));\n}\n\nfn main() {\n    // Create an object\n    let person = person::Person {\n        name: "Alexey".to_string(),\n        age: 30,\n        phone_numbers: vec!["+7-999-123-45-67".to_string()],\n    };\n\n    // Serialize to bytes\n    let mut buf = Vec::new();\n    person.encode(&mut buf).unwrap();\n    println!("Serialized {} bytes", buf.len());\n\n    // Deserialize back\n    let decoded = person::Person::decode(&buf[..]).unwrap();\n    println!("Name: {}, Age: {}", decoded.name, decoded.age);\n}
\n\n

Core Methods and Functions

\n\n

1. Message::encode

\n

What it does: Serializes a message into protobuf binary format and writes it to a buffer.

\n

Parameters:

\n
    \n
  • &self — reference to the message
  • \n
  • buf: &mut impl BufMut — buffer for writing (usually Vec<u8>)
  • \n
\n

Example:

\n
let mut buf = Vec::new();\nmy_message.encode(&mut buf).unwrap();
\n\n

2. Message::decode

\n

What it does: Deserializes bytes back into a Rust structure.

\n

Parameters:

\n
    \n
  • buf: impl Buf — byte source (slice, Vec, Bytes)
  • \n
\n

Example:

\n

                
    
    

Recommendations