Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. //! An example showing off the usage of `Deserialize` to automatically decode
  2. //! TOML into a Rust `struct`
  3.  
  4. #![deny(warnings)]
  5.  
  6. extern crate toml;
  7. #[macro_use]
  8. extern crate serde_derive;
  9.  
  10. #[derive(Debug, Deserialize)]
  11. struct Tasks {
  12. items: Option<Vec<Task>>,
  13. }
  14.  
  15. #[derive(Deserialize, Debug)]
  16. struct Task {
  17. id: String,
  18. field: String,
  19. }
  20.  
  21. fn main() {
  22. let toml_str = r#"
  23. [[items]]
  24. id = "todo"
  25. field = "something"
  26. [[items]]
  27. id = "todo"
  28. field = "something"
  29.  
  30. "#;
  31.  
  32. let entry = Task { id: "1".to_string(), field: "Test encoding".to_string() };
  33.  
  34. let decoded: Tasks = toml::from_str(toml_str).unwrap();
  35. let encoded = toml::to_string(&entry).unwrap();
  36. println!("{:#?}", encoded);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement