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

I'm using lettre, and in the exmaple:
... 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();
    let tls_parameters =
        ClientTlsParameters::new(smtp_server.clone(), tls_builder.build()
        (smtp_server.as_str(), smtp_port),  ClientSecurity::Wrapper(tls_parameters))
        .credentials(smtp::authentication::Credentials::new(smtp_username, smtp_password))

and I call that from a different class:

let result = smtp::SMTP_CLIENT.send(email);

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.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.