假如要返回一个结构体中的一个字段,例如返回response结构体中的headers字段,但并不希望clone一份这个字段,因为response中其他字段我不需要了,我只需要headers字段, 以下这段代码是编译不过的,可以通过mem::replace 将struct中的headers替换出来
fn just_the_headers() -> Result<hyper::header::Headers, hyper::error::Error> {
let c = hyper::client::Client::new();
let result = c.get("http://www.example.com").send();
match result {
Err(e) => Err(e),
Ok(response) => Ok(response.headers),
}
}
fn main() {
println!("{:?}", just_the_headers());
}
main.rs:8:28: 8:44 error: cannot move out of type `hyper::client::response::Response`, which defines the `Drop` trait
main.rs:8 Ok(response) => Ok(response.headers),
^~~~~~~~~~~~~~~~
error: aborting due to previous error
fn just_the_headers() -> Result<hyper::header::Headers, hyper::error::Error> {
let c = hyper::client::Client::new();
let result = c.get("http://www.example.com").send();
match result {
Err(e) => Err(e),
Ok(mut response) => Ok(std::mem::replace(&mut response.headers, hyper::header::Headers::new())),
}
}
fn main() {
println!("{:?}", just_the_headers());
}