rustHow to write YAML to file in Rust
Writing YAML to file in Rust is easy with the serde_yaml crate.
Example code:
use serde_yaml;
let data = vec![1, 2, 3];
let serialized = serde_yaml::to_string(&data).unwrap();
std::fs::write("data.yaml", serialized).unwrap();
Output example
[1, 2, 3]
Code parts:
use serde_yaml;
- imports the serde_yaml crate.let data = vec![1, 2, 3];
- creates a vector of data to be serialized.let serialized = serde_yaml::to_string(&data).unwrap();
- serializes the data into a YAML string.std::fs::write("data.yaml", serialized).unwrap();
- writes the serialized data to a file nameddata.yaml
.
Helpful links
- serde_yaml - crate documentation.
- std::fs::write - documentation for the
write
function.
Related
More of Rust
- How to split a string by regex in Rust?
- How to use regex with bytes in Rust?
- How to replace a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to create a HashSet from a Vec in Rust?
- How to use the global flag in a Rust regex?
- How to escape dots with regex in Rust?
- How to declare a matrix in Rust?
- How to implement PartialEq for a Rust HashMap?
- How to get an entry from a HashSet in Rust?
See more codes...