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::Valueobject.println!("Name: {}", data["name"].as_str().unwrap()): This prints the value of thenamekey as a string.println!("Age: {}", data["age"].as_i64().unwrap()): This prints the value of theagekey as an integer.
Helpful links
Related
More of Rust
- How to replace a capture group using Rust regex?
- Weak pointer example in Rust
- How to use regex lookbehind in Rust?
- How to perform matrix operations in Rust?
- How to split a string with Rust regex?
- How to match whitespace with a regex in Rust?
- How to use regex to match a double quote in Rust?
- How to use regex captures in Rust?
- How to use non-capturing groups in Rust regex?
- Regex example to match multiline string in Rust?
See more codes...