doc_example.rs 831 B

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