假如要返回一个结构体中的一个字段,例如返回response结构体中的headers字段,但并不希望clone一份这个字段,因为response中其他字段我不需要了,我只需要headers字段, 以下这段代码是编译不过的,可以通过mem::replace 将struct中的headers替换出来

    1. fn just_the_headers() -> Result<hyper::header::Headers, hyper::error::Error> {
    2. let c = hyper::client::Client::new();
    3. let result = c.get("http://www.example.com").send();
    4. match result {
    5. Err(e) => Err(e),
    6. Ok(response) => Ok(response.headers),
    7. }
    8. }
    9. fn main() {
    10. println!("{:?}", just_the_headers());
    11. }
    12. main.rs:8:28: 8:44 error: cannot move out of type `hyper::client::response::Response`, which defines the `Drop` trait
    13. main.rs:8 Ok(response) => Ok(response.headers),
    14. ^~~~~~~~~~~~~~~~
    15. error: aborting due to previous error
    1. fn just_the_headers() -> Result<hyper::header::Headers, hyper::error::Error> {
    2. let c = hyper::client::Client::new();
    3. let result = c.get("http://www.example.com").send();
    4. match result {
    5. Err(e) => Err(e),
    6. Ok(mut response) => Ok(std::mem::replace(&mut response.headers, hyper::header::Headers::new())),
    7. }
    8. }
    9. fn main() {
    10. println!("{:?}", just_the_headers());
    11. }