Rahul Gurnani
Rahul Gurnani

Reputation: 194

Client certificate in Rust using Hyper

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?

Upvotes: 1

Views: 1280

Answers (1)

AlexeyKarasev
AlexeyKarasev

Reputation: 520

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();

Upvotes: 2

Related Questions