0PricingLogin
Learn Rust Coding · Lesson

Generating Code with tonic-build

Compile proto into Rust.

What tonic-build Does

tonic-build runs at compile time and turns your .proto files into Rust source. It wraps prost-build for messages and adds the gRPC service traits and clients on top.

It runs from a Cargo build script (build.rs), so generation happens automatically before your crate compiles.

Cargo Dependencies

You need runtime crates and a build-time crate. tonic and prost are normal dependencies; tonic-build goes under [build-dependencies].

tonic relies on tokio as its async runtime, so include it too.

[dependencies]
tonic = "0.12"
prost = "0.13"
tokio = { version = "1", features = ["full"] }

[build-dependencies]
tonic-build = "0.12"

All lessons in this course

  1. Protobuf and Service Definitions
  2. Generating Code with tonic-build
  3. Implementing a gRPC Server
  4. Calling from a gRPC Client
← Back to Learn Rust Coding