Rust中使用Protocol Buffers

栏目: 编程语言 · Rust · 发布时间: 4年前

内容简介:Google Protocol Buffer(简称 Protobuf)是一种轻便高效的结构化数据存储格式,平台无关、语言无关、可扩展,可用于通讯协议和数据存储等领域。下面给出在Rust中使用Protocol Buffers的基本流程步骤。下面以Ubuntu-16.04LTS为例:0.预先安装

Google Protocol Buffer(简称 Protobuf)是一种轻便高效的结构化数据存储格式,平台无关、语言无关、可扩展,可用于通讯协议和数据存储等领域。下面给出在Rust中使用Protocol Buffers的基本流程步骤。

下面以Ubuntu-16.04LTS为例:

一、安装protoc

0.预先安装

sudo apt-get install autoconf automake libtool curl make g++ unzip
复制代码

1.获取源码,生成configure

git clone https://github.com/google/protobuf.git
cd protobuf
git submodule update --init --recursive
./autogen.sh
复制代码

2.编译安装

./configure  #By default, the package will be installed to /usr/local
make
make check
sudo make install
sudo ldconfig # refresh shared library cache.
复制代码

安装步骤可参考: github.com/google/prot…

二、安装protoc-gen-rust插件

使用cargo 安装:

cargo install protobuf --vers 1.7.4 #1.7.4为版本号,可选填。默认安装到~/.cargo/bin目录中
复制代码

还可使用源码安装,从github上clone源码,编译安装,加入环境变量。安装步骤可参考: github.com/stepancheg/…

三、编写proto文件生成对应rust文件

proto文件语法规则可参考: Language Guide (proto3)

举例说明(在当前目录下生成foo.proto对应的rust文件):

protoc --rust_out . foo.proto 
复制代码

如果是其他语言,可在 Third-Party Add-ons for Protocol Buffers 中找相关语言的插件等。

四、工程应用

  1. 在rust工程中Cargo.toml中的添加protobuf
[dependencies]
protobuf = "1.7"		//注意版本问题,1.x与2.x,同时这里的版本 须与上面安装的protobuf版本相一致
复制代码
  1. 添加引用的crate:
extern crate protobuf;
复制代码
  1. 引用相关api......

学习文档:Developer Guide

     


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learn Python the Hard Way

Learn Python the Hard Way

Zed Shaw / Example Product Manufacturer / 2011

This is a very beginner book for people who want to learn to code. If you can already code then the book will probably drive you insane. It's intended for people who have no coding chops to build up t......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具