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
- Rust map function example
- How to create a HashMap of structs in Rust?
- How to use named capture groups in Rust regex?
- How to perform matrix operations in Rust?
- How to use regex lookbehind in Rust?
- How to find the first match in a Rust regex?
- How do I clone a string in Rust?
- How to use regex lookahead in Rust?
- How to convert JSON to a struct in Rust?
- How to get the first element of a slice in Rust?
See more codes...