iterate over yaml array in rust

Viewed 612

Hi i have a yaml array that i would like to iterate over in rust this is the yaml array:

schedules: 
  - 8:00-9:10
  - 9:30-10:40
  - 11:00-12:10
  - 16:20-17:30
  - 15:00-16-10

and this is the code i have already got:

extern crate yaml_rust;
use std::fs::read_to_string;
use yaml_rust::{YamlLoader};
fn main() {
    let config = read_to_string("config.yaml").unwrap();
    let docs = YamlLoader::load_from_str(&config).unwrap();
    let doc = &docs[0];
    let schedules = &doc["schedules"];
}

how would I go about iterating schedules?

i have tried turning it to a hash map (using .as_hash().unwrap() as other posts i saw suggested) but that says: thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:8:49

1 Answers

yaml_rust represents arrays as vectors. You can convert a YAML node into a Vec with the as_vec method. If the node is not an array, as_vec will return None:

for schedule in schedules.as_vec().unwrap() {
    schedule.do_stuff()
}
Related