Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 19, 2018 06:43
Show Gist options
  • Save rust-play/dd4fc176b02a74fe4e663ef600f4b943 to your computer and use it in GitHub Desktop.
Save rust-play/dd4fc176b02a74fe4e663ef600f4b943 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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