我正在尝试转换结构列表并以 BSON 格式写入文件并反序列化回 struct 。我在用
bson = { version="2.6.0" }
板条箱。我将我的结构列表转换为 Bson ,然后尝试找到任何方法将 bson 转换为 vec[u8] ,但没有找到任何 .
我只需要将二进制格式的 bson 存储到特定文件中。
这是代码片段
use std::{fs::File, io::Write};
use serde::{Serialize, Deserialize};
#[derive(Debug,Serialize,Deserialize)]
struct Bison {
name: String,
age: u16,
place: String,
phone: u16,
}
pub fn check_bson() {
let mut bisons: Vec<Bison> = Vec::with_capacity(1000);
for i in 1..1001 {
bisons.push(Bison {
name: format!("Name {}", i),
age: i as u16,
place: format!("Place {}", i),
phone: i as u16,
});
}
let mut file = File::create("data.bson").expect("Failed to create file");
let s = bson::to_bson(&bisons).unwrap();
}