rust 使用 protobuf

没有技术含量的东西,就是记录一下流水帐。

crate.io 上搜索 protobuf:

https://github.com/stepancheg/rust-protobuf

1、下载 google 的 protoc 解压后添加至环境变量

https://github.com/google/protobuf/releases

2、安装 protoc-gen-rust,可以使用命令:

cargo install protobuf

会安装在 C:\Users\Administrator\.cargo\bin 目录下,同样添加至环境变量。

3、使用protoc生成对应的 rust 原码文件

protoc --rust_out . foo.proto

4、在rust工程中Cargo.toml中的添加protobuf

[dependencies]
protobuf = "1.3.1"

5、添加引用的crate:

extern crate protobuf;

6、测试代码:

extern crate protobuf;
use protobuf::Message;

let mut hello=common::Hello::new();
hello.set_id(4);

let data=protobuf::Message::write_to_bytes(&hello).expect("error");
let data2=hello.write_to_bytes().expect("error");
println!("{:?}",data);
println!("{:?}",data2);

let hello=protobuf::parse_from_bytes::<common::Hello>(&data).expect("error");
println!("{}",hello.get_id());
发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>