Expected struct "RwLockWriteGuard", found enum "Result"

How to fix this build error:

error[E0308]: mismatched types
  --> src/main.rs:38:16
   |
38 |         sync_error = Err(format!("{:?}", error));
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::sync::RwLockWriteGuard`, found enum `std::result::Result`
   |
   = note: expected type `std::sync::RwLockWriteGuard<'_, std::result::Result<std::string::String, _>, >`
              found type `std::result::Result<_, _>`

I simplify the code to describe the problem and put it in the rust playground , please somebody help!

Also I paste code here:

use std::sync::RwLock;

#[derive(Debug)]
pub enum Error1 {
	InvalidTotalKernelSum,
	Other(String)
}

#[derive(Debug)]
pub enum Error2 {
	NotFoundErr(String),
	SerErr(String),
}


#[derive(Debug)]
pub enum Error {
	Core(Error1),
	Store(Error2),
}

#[derive(Debug)]
pub struct SyncState {
	sync_error: RwLock<Result<String, String>>,
}

impl SyncState {
	pub fn new() -> SyncState {
		SyncState {
			sync_error: RwLock::new(Ok("None".to_owned())),
		}
    }
    
    /// Communicate sync error
	pub fn set_sync_error(&self, error: Error){
		let mut sync_error = self.sync_error.write().unwrap();
		sync_error = Err(format!("{:?}", error));
	}

}

fn main() {
    let state = SyncState::new();
    println!("state={:?}", state);
}

You want to store the Err inside the value protected by the guard (mutex), so need a deref:

*sync_error = Err(format!("{:?}", error));
1 Like

Great thanks~ It works:clap: