pub fn compile_protos(
    protos: &[impl AsRef<Path>],
    includes: &[impl AsRef<Path>]
) -> Result<()>
Expand description

Compile .proto files into Rust files during a Cargo build.

The generated .rs files are written to the Cargo OUT_DIR directory, suitable for use with the include! macro. See the Cargo code generation example for more info.

This function should be called in a project’s


protos - Paths to .proto files to compile. Any transitively imported .proto files are automatically be included.

includes - Paths to directories in which to search for imports. Directories are searched in order. The .proto files passed in protos must be found in one of the provided include directories.


This function can fail for a number of reasons:

  • Failure to locate or download protoc.
  • Failure to parse the .protos.
  • Failure to locate an imported .proto.
  • Failure to compile a .proto without a package specifier.

It’s expected that this function call be unwraped in a; there is typically no reason to gracefully recover from errors during a build.


fn main() -> Result<()> {
  prost_build::compile_protos(&["src/frontend.proto", "src/backend.proto"], &["src"])?;