collection_data_example.rs 1017 B

12345678910111213141516171819202122232425
  1. use shared::Data;
  2. use std::collections::HashMap;
  3. fn main() {
  4. // Let's quickly populate a map
  5. let mut map: HashMap<String, Data> = HashMap::from([
  6. ("gravity".to_string(), Data::from(9.81_f32)),
  7. ("life".to_string(), Data::from(42_i32)),
  8. ("cat".to_string(), Data::from("meow".to_string())),
  9. ]);
  10. // Hmm... ok, looks good.
  11. map.insert("direct_insert".to_string(), Data::Bool(true));
  12. // Now to stuff it into a single thing...
  13. let map: Data = Data::from(map);
  14. // Poof! (Now as of v0.1.1 we can't directly insert or get out, as Vec and HashMap have different parameters)
  15. // Ok let's pull it all back out
  16. let m: Result<HashMap<String, Data>, ()> = map.clone().try_into();
  17. if let Ok(m) = m {
  18. println!("m is {:?}", m);
  19. } else {
  20. println!("{:?}", map);
  21. }
  22. // Notice: Even with `cast_into` feature, this type of try_into will fail on any other Data variant (Capt. Obvious: Well yeah, the others are primitives so we could cast them)
  23. }