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 replace a capture group using Rust regex?
- How to match a URL with a regex in Rust?
- How to match the end of a line in a Rust regex?
- How to use an enum in a Rust HashMap?
- How to replace all matches using Rust regex?
- How to declare a Rust slice?
- How to get a capture group using Rust regex?
- How to match all using regex in Rust?
- How to get an element from a HashSet in Rust?
- How to convert a Rust HashMap to JSON?
See more codes...