doc_example.rs 786 B

12345678910111213141516171819202122232425262728293031323334353637
  1. use shared::Shared;
  2. #[derive(Clone)]
  3. struct Person {
  4. pub name: String,
  5. pub age: u16,
  6. }
  7. // Using it as a parameter
  8. async fn get_older(shared: Shared<Person>) {
  9. let mut person = shared.lock().await;
  10. person.age += 1;
  11. }
  12. #[tokio::main]
  13. async fn main() {
  14. let shared_data = Shared::new(Person {
  15. name: "Test Dummy".to_string(),
  16. age: 30,
  17. });
  18. {
  19. // Direct access
  20. let person = shared_data.lock().await;
  21. println!("Before:");
  22. println!("Person{{name: {}, age: {}}}", person.name, person.age);
  23. }
  24. // Calling
  25. get_older(shared_data.clone()).await;
  26. {
  27. let person = shared_data.lock().await;
  28. println!("After:");
  29. println!("Person{{name: {}, age: {}}}", person.name, person.age);
  30. }
  31. }