12345678910111213141516171819202122232425262728293031323334353637 |
- use shared::Shared;
- #[derive(Clone)]
- struct Person {
- pub name: String,
- pub age: u16,
- }
- // Using it as a parameter
- async fn get_older(shared: Shared<Person>) {
- let mut person = shared.lock().await;
- person.age += 1;
- }
- #[tokio::main]
- async fn main() {
- let shared_data = Shared::new(Person {
- name: "Test Dummy".to_string(),
- age: 30,
- });
- {
- // Direct access
- let person = shared_data.lock().await;
- println!("Before:");
- println!("Person{{name: {}, age: {}}}", person.name, person.age);
- }
- // Calling
- get_older(shared_data.clone()).await;
- {
- let person = shared_data.lock().await;
- println!("After:");
- println!("Person{{name: {}, age: {}}}", person.name, person.age);
- }
- }
|