"cannot borrow data in a & reference as mutable" error

I'm using lettre, and in the exmaple:
https://lettre.at/sending-messages/smtp.html
... it says "let mut mailer = SmtpClient::new ..."

However, I have code in the smtp.rs like this:

lazy_static! {
    pub static ref SMTP_CLIENT: SmtpTransport = init_smtp_client();
}

fn init_smtp_client() -> SmtpTransport {
    let smtp_server = env::var("SMTP_SERVER").expect("SMTP_SERVER must be set");
    let smtp_port = env::var("SMTP_PORT").expect("SMTP_PORT must be set").parse().unwrap();
    let smtp_username = env::var("SMTP_USERNAME").expect("SMTP_USERNAME must be set");
    let smtp_password = env::var("SMTP_PASSWORD").expect("SMTP_PASSWORD must be set");
    let mut tls_builder = TlsConnector::builder();
    tls_builder.min_protocol_version(Some(Protocol::Tlsv10));
    let tls_parameters =
        ClientTlsParameters::new(smtp_server.clone(), tls_builder.build()
        .unwrap());
    SmtpClient::new(
        (smtp_server.as_str(), smtp_port),  ClientSecurity::Wrapper(tls_parameters))
        .unwrap()
        .authentication_mechanism(smtp::authentication::Mechanism::Login)
        .credentials(smtp::authentication::Credentials::new(smtp_username, smtp_password))
        .connection_reuse(smtp::ConnectionReuseParameters::ReuseUnlimited)
        .transport()
}

and I call that from a different class:

let result = smtp::SMTP_CLIENT.send(email);
assert!(result.is_ok());
smtp::SMTP_CLIENT.close();

and I got this compile error: "cannot borrow data in a & reference as mutable".

 |     let result = smtp::SMTP_CLIENT.send(email);
 |                  ^^^^^^^^^^^^^^^^^ cannot borrow as mutable

How can I resolve this?

1 Like

Here is an example

lazy_static::lazy_static! {
        pub static ref MUTIE: Arc<Mutex<Vec<u8>>> = Arc::new(Mutex::new(vec![]));
    }

and to test it out playground

1 Like

You don't want or need the Arc, just the Mutex will do.

2 Likes