我一直在Rust中编写一个客户端,它向具有客户端证书的服务器发出请求(Pkcs12)。尽管已经回答How to make a request with client certificate in Rust,但代码并没有按原样编译。如果我做一些修改,比如替换'?'通过调用unwrap()函数,
代码:
let tls_conn = TlsConnector::builder().unwrap()
.identity(cert).unwrap()
.build().unwrap();
错误:
let tls_conn = TlsConnector::builder().unwrap()
| ____________________^
18 | | .identity(cert).unwrap()
| |________________________________^ cannot move out of borrowed content.
我重写了上面的代码行并将其分解为多行进行调试:
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();
我得到如下错误:
let tls_conn = tls_conn_builder.build().unwrap();
| ^^^^^^^^^^^^^^^^ cannot move out of borrowed content.
我是Rust的新手并在此寻求帮助,有人可以分享编译的示例代码吗?
答案 0 :(得分:1)
此处您不需要任何mut
引用。构建器模式是创建smth可变(TlsConnector::builder().unwrap()
),变异(tls_conn_builder.identity(cert)
)然后获得结果(build
)。试试这段代码
let mut tls_conn_builder = TlsConnector::builder().unwrap();
tls_conn_builder.identity(cert);
let tls_conn = tls_conn_builder.build().unwrap();