I have a quick stylistic or idiomatic question about Mutex.
Is there a more elegant way to modify the data in fn upper() than using *string = match *string. It seems odd to dereference on both side, but if I don't, I don't know how to handle the <MutexGuard> part of string.
Link to playground
use std::sync::{Mutex};
#[derive(Debug)]
struct SharedFile{
file: Mutex<Option<String>>
}
impl SharedFile{
fn new()-> SharedFile{
SharedFile{
file: Mutex::new(Some("test".to_owned())),
//file: Mutex::new(None),
}
}
fn upper(&self){
let mut string = self.file.lock().unwrap();
*string= match *string{
Some(ref mut x) => Some(x.to_uppercase()),
None => Some("Empty".to_owned()),
};
println!("{:?}", *string);
}
}
fn main() {
let shared = SharedFile::new();
shared.upper();
println!("{:?}", shared);
}