rustRust YAML parser example
serde_yaml is a Rust library for parsing and serializing YAML. Here is an example of how to parse a YAML string:
use serde_yaml;
let yaml_str = "
name: John
age: 30
";
let data: serde_yaml::Value = serde_yaml::from_str(yaml_str).unwrap();
println!("Name: {}", data["name"].as_str().unwrap());
println!("Age: {}", data["age"].as_i64().unwrap());
This code will ## Output example
Name: John
Age: 30
The code consists of the following parts:
use serde_yaml
: This imports the serde_yaml library.let yaml_str = ...
: This creates a string containing the YAML data.let data: serde_yaml::Value = serde_yaml::from_str(yaml_str).unwrap()
: This parses the YAML string and stores the result in aserde_yaml::Value
object.println!("Name: {}", data["name"].as_str().unwrap())
: This prints the value of thename
key as a string.println!("Age: {}", data["age"].as_i64().unwrap())
: This prints the value of theage
key as an integer.
Helpful links
Related
More of Rust
- How to use regex to match a double quote in Rust?
- How to get a capture group using Rust regex?
- How to implement PartialEq for a Rust HashMap?
- How to convert a Rust HashMap to a BTreeMap?
- How to use regex with bytes in Rust?
- How to parse JSON string in Rust?
- How to use regex to match a group in Rust?
- How to calculate the inverse of a matrix in Rust?
- How to use a tuple as a key in a Rust HashMap?
- Example of yield_now in Rust?
See more codes...