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.
32 lines
637 B
32 lines
637 B
2 years ago
|
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());
|
||
|
}
|