Julio Biason
3 years ago
3 changed files with 46 additions and 0 deletions
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo. |
||||
# It is not intended for manual editing. |
||||
version = 3 |
||||
|
||||
[[package]] |
||||
name = "rwlocktest" |
||||
version = "0.1.0" |
@ -0,0 +1,8 @@
|
||||
[package] |
||||
name = "rwlocktest" |
||||
version = "0.1.0" |
||||
edition = "2021" |
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||
|
||||
[dependencies] |
@ -0,0 +1,31 @@
|
||||
use std::sync::RwLock; |
||||
|
||||
struct Holder { |
||||
content: RwLock<String>, |
||||
} |
||||
|
||||
impl Holder { |
||||
fn new() -> Self { |
||||
Self { |
||||
content: RwLock::new(String::from("hello")), |
||||
} |
||||
} |
||||
|
||||
fn change(&self, content: &str) { |
||||
let mut original = self.content.write().unwrap(); |
||||
*original = content.into(); |
||||
} |
||||
|
||||
fn content(&self) -> String { |
||||
let lock = self.content.read().unwrap(); |
||||
String::from(&*lock) |
||||
} |
||||
} |
||||
|
||||
fn main() { |
||||
let content = Holder::new(); |
||||
println!("Content: {}", content.content()); |
||||
|
||||
content.change("Hello there"); |
||||
println!("Content: {}", content.content()); |
||||
} |
Loading…
Reference in new issue