1

I have been writing a client in Rust which makes a request to a server with a client certificate (Pkcs12). Although this has been answered How to make a request with client certificate in Rust, the code doesn't compile as it is. If I make some modifications like replacing '?' by call to unwrap() function,

Code:

let tls_conn = TlsConnector::builder().unwrap()
        .identity(cert).unwrap()
        .build().unwrap();

Error:

let tls_conn = TlsConnector::builder().unwrap()
   |  ____________________^
18 | |         .identity(cert).unwrap()
   | |________________________________^ cannot move out of borrowed content.

I rewrote the above line of code and broke it down into multiple lines for debugging:

let ref mut init_tls_conn_builder = TlsConnector::builder().unwrap();
let ref mut tls_conn_builder = init_tls_conn_builder.identity(cert).unwrap();
let tls_conn = tls_conn_builder.build().unwrap();

I get the error as follows:

let tls_conn = tls_conn_builder.build().unwrap();
   |                        ^^^^^^^^^^^^^^^^ cannot move out of borrowed content.

I am new to Rust and seeking help on this, can anyone please share an example code which compiles?

Simone
  • 20,302
  • 14
  • 79
  • 103
Rahul Gurnani
  • 174
  • 1
  • 13
  • 1
    Possible duplicate of [Cannot move out of borrowed content when unwrapping a box](https://stackoverflow.com/questions/32338659/cannot-move-out-of-borrowed-content-when-unwrapping-a-box) – ljedrz Apr 23 '18 at 08:19

1 Answers1

2

You don't need any mut references here. The builder pattern is create smth mutable (TlsConnector::builder().unwrap()), mutate it (tls_conn_builder.identity(cert)) and then get the result (build). Try this code

let mut tls_conn_builder = TlsConnector::builder().unwrap();
tls_conn_builder.identity(cert);
let tls_conn = tls_conn_builder.build().unwrap();
AlexeyKarasev
  • 490
  • 3
  • 13