You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a `build.rs` file at the root of your crate. A build.rs script is a Rust program that Cargo executes before compiling your main project. Its purpose is to perform tasks like generating source code, linking to non-Rust libraries, or setting environment variables that influence the build process.
89
+
90
+
In this case, we will be putting the command to compile and build the `.proto` file in build.rs. We will use gRPC's tonic_protobuf_build crate to generate code from the `.proto` file.
91
+
```rust
92
+
fnmain() {
93
+
tonic_protobuf_build::CodeGen::new()
94
+
.include("proto")
95
+
.inputs(["routeguide.proto"])
96
+
.output_dir("generated")
97
+
.compile()
98
+
.unwrap();
99
+
}
100
+
```
101
+
Now, run
102
+
```shell
103
+
$ cargo build
104
+
```
105
+
106
+
## Bringing Generated Code into Scope
107
+
108
+
The generated code is placed inside our target directory, in a location defined by the `OUT_DIR`
109
+
environment variable that is set by cargo. For our example, this means you can find the generated
110
+
code in a path similar to `target/debug/build/routeguide/out/routeguide.rs`.
111
+
112
+
We can use gRPC's `include_proto` macro to bring the generated code into scope:
113
+
114
+
```rust
115
+
pubmodrouteguide {
116
+
grpc::include_proto!("routeguide");
117
+
}
118
+
```
119
+
120
+
**Note**: The token passed to the `include_proto` macro (in our case "routeguide") is the name of
121
+
the package declared in our `.proto` file, not a filename, e.g "routeguide.rs".
122
+
123
+
With this in place, we can stub out our service implementation:
0 commit comments