Created
June 19, 2018 06:43
-
-
Save rust-play/dd4fc176b02a74fe4e663ef600f4b943 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::{RwLock, Arc}; | |
use std::option::Option; | |
#[derive(Debug)] | |
struct Tomato{ | |
salad: std::string::String, | |
} | |
fn main() { | |
let optional_rwlock: Arc<RwLock<Option<Tomato>>> = Arc::new(RwLock::new(None)); | |
let clone = optional_rwlock.clone(); | |
clone.read().ok().and_then(|option|{ | |
println!("{:?}: {:?}", option, *option); | |
Some(1) | |
}); | |
optional_rwlock.write().ok().and_then(|mut option|{ | |
*option = Some(Tomato { salad: "Spaghett".to_string() }); | |
println!("{:?}: {:?}", option, *option); | |
Some(1) | |
}); | |
clone.read().ok().and_then(|option|{ | |
println!("{:?}: {:?}", option, *option); | |
Some(1) | |
}); | |
optional_rwlock.write().ok().and_then(|option|{ | |
println!("{:?}: {:?}", option, *option); | |
Some(1) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment