構建 Rust Protos

描述如何使用 Cargo 或 Bazel 構建 Rust 原型。

Cargo

請參閱 protobuf-example crate,瞭解如何設定構建的示例。

Bazel

為 Protobuf 定義構建 Rust 庫的過程與其他程式語言類似

  1. 使用與語言無關的 proto_library 規則

    proto_library(
        name = "person_proto",
        srcs = ["person.proto"],
    )
    
  2. 建立一個 Rust 庫

    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. 透過將其包含在 Rust 二進位制檔案中來使用該庫

    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",
        ],
    )