You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
637 B
31 lines
637 B
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()); |
|
}
|
|
|