1234567891011121314151617181920212223242526272829303132333435363738 |
- use shared::Shared;
- // #[derive(Clone)]
- // Clone is no longer needed as of v0.2.0
- 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);
- }
- }
|