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
- Protobuf and Service Definitions
- Generating Code with tonic-build
- Implementing a gRPC Server
- Calling from a gRPC Client