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 strings using Rust regex?
- How to parse JSON string in Rust?
- How to compile a regex in Rust?
- How to perform matrix operations in Rust?
- How to convert struct to JSON string in Rust?
- How to add matrices in Rust?
- How to replace a capture group using Rust regex?
- How to multiply matrices in Rust?
- How to use regex builder in Rust?
- Hashshet example in Rust
See more codes...