Building Rust Protos

Describes how to build Rust protos using Cargo or Bazel.

Cargo

See the protobuf-example crate for an example of how to set up your build.

Bazel

The process of building a Rust library for a Protobuf definition is similar to other programming languages:

  1. Use the language-agnostic proto_library rule:

    proto_library(  name = "person_proto",  srcs = ["person.proto"], ) 
  2. Create a Rust library:

    load("//third_party/protobuf/rust:defs.bzl", "rust_proto_library")  proto_library(  name = "person_proto",  srcs = ["person.proto"], )  rust_proto_library(  name = "person_rust_proto",  deps = [":person_proto"], ) 
  3. Use the library by including it in a Rust binary:

    load("//third_party/bazel_rules/rules_rust/rust:defs.bzl", "rust_binary") load("//third_party/protobuf/rust:defs.bzl", "rust_proto_library")  proto_library(  name = "person_proto",  srcs = ["person.proto"], )  rust_proto_library(  name = "person_rust_proto",  deps = [":person_proto"], )  rust_binary(  name = "greet",  srcs = ["greet.rs"],  deps = [  ":person_rust_proto",  ], )