protoc-rs
is a simple standalone command line tool for generating a usable
Rust crate from Protobuf .proto
files. It is not a Rust implementation of
protoc but rather a supplement to it when a set of
Protobuf definitions need to be compiled into multiple languages and Rust being one of them.
The Cargo manifest file, Cargo.toml
must exist (it will not be generated). It must depend on
tonic and prost, e.g.
[dependencies]
tonic = "0"
prost = "0"
If the repository contains protobuf generated files for multiple languages, it is recommended to
put the generated Rust files into a subdirectory called rust/
instead of the usual source path
src/
. That makes it clear that this directory contains the generated Rust code, while other
subdirectories contain other language code (e.g. go/
, cpp/
, ...). To make this work and make the
crate importable, the custom source code location must also be set in the Cargo.toml
. For example:
[lib]
path = "rust/lib.rs"
The root library file is always the default name lib.rs
.
- Navigate to the crates root directory (e.g. where
Cargo.toml
is located). - Compile the
.proto
files. The command works similar toprotoc
. If they are located underproto/
, the command would look like this:All compiled proto files must be passed to a single call of the command so theprotoc-rs -I proto/ --out=rust/ proto/*.proto
lib.rs
file can be generated accordingly. Also, all source files must be put under one of the specified include paths.
Now, the rust/
directory contains a valid Rust module structure and the crate can be imported by
other crates. You can validate the crate by running cargo check
.