مثال آخر لعملية الـ Serialization و الـ Deserializtion في لغة Rust باستعمال المكتبة المشهورة serde.
في المثال قمت بتحويل مصفوفة من النوع Book إلى تنسيق JSON وكذلك Yaml
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate serde_yaml;
#[derive(Serialize, Deserialize)]
struct Book {
title: String,
price: f32,
author: Vec<String>,
pages: i32,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
}
fn main() {
let rust_book = Book {
title: "Programming Rust".into(),
price: 48.05,
author: vec!["Jim Blandy".into(), "Jason Orenddorff".into()],
pages: 606,
description: Some(
"Rust is a new systems programming language that combines the performance and \
low-level control of C and C++ with memory safety and thread safety. Rust's modern, \
flexible types ensure your program is free of null pointer dereferences, double \
frees, dangling pointers, and similar bugs, all at compile time, without runtime \
overhead. In multi-threaded code, Rust catches data races at compile time, making \
concurrency much easier to use."
.into(),
),
};
let ddd_book = Book {
title: "Domain-Driven Design : Tackling Complexity in the Heart of Software".into(),
price: 64.88,
author: vec!["Eric Evans".into()],
pages: 560,
description: None,
};
let books = vec![rust_book, ddd_book];
println!(
"Here is the JSON format for these books: \n{}",
serde_json::to_string_pretty(&books).unwrap()
);
println!(
"Here is the Yaml format for these books: \n{}",
serde_yaml::to_string(&books).unwrap()
);
}
وهنا المخرجات بعد تشغيل البرنامج:
~/أعمالي/البرمجة/Rust/learrning_rust $ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/learrning_rust`
Here is the JSON format for these books:
[
{
"title": "Programming Rust",
"price": 48.05,
"author": [
"Jim Blandy",
"Jason Orenddorff"
],
"pages": 606,
"description": "Rust is a new systems programming language that combines the performance and low-level control of C and C++ with memory safety and thread safety. Rust's modern, flexible types ensure your program is free of null pointer dereferences, double frees, dangling pointers, and similar bugs, all at compile time, without runtime overhead. In multi-threaded code, Rust catches data races at compile time, making concurrency much easier to use."
},
{
"title": "Domain-Driven Design : Tackling Complexity in the Heart of Software",
"price": 64.88,
"author": [
"Eric Evans"
],
"pages": 560
}
]
Here is the Yaml format for these books:
---
- title: Programming Rust
price: 48.04999923706055
author:
- Jim Blandy
- Jason Orenddorff
pages: 606
description: "Rust is a new systems programming language that combines the performance and low-level control of C and C++ with memory safety and thread safety. Rust's modern, flexible types ensure your program is free of null pointer dereferences, double frees, dangling pointers, and similar bugs, all at compile time, without runtime overhead. In multi-threaded code, Rust catches data races at compile time, making concurrency much easier to use."
- title: "Domain-Driven Design : Tackling Complexity in the Heart of Software"
price: 64.87999725341797
author:
- Eric Evans
pages: 560