How to fix this 'second mutable borrow occurs here' error

Hello, I'm new to Rust. I'm trying to fix the following 'second mutable borrow occurs here' error.
Basically I wanted to write a function that uses httparse to parse a buffer as a Response and if it fails to parse it as Request. Also, I wanted to have a single httparse::Header array. Any suggestions?

Playground link: Rust Playground

The error:

   Compiling tmp v0.1.0 (file:///home/oblique/scratchpad/rust/tmp)
error[E0499]: cannot borrow `*headers` as mutable more than once at a time
  --> src/main.rs:17:46
   |
10 |         let mut res = httparse::Response::new(headers);
   |                                               ------- first mutable borrow occurs here
...
17 |         let mut req = httparse::Request::new(headers);
   |                                              ^^^^^^^ second mutable borrow occurs here
...
24 | }
   | - first borrow ends here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0499`.
error: Could not compile `tmp`.

To learn more, run the command again with --verbose.

My code:

extern crate httparse;

enum Http<'headers, 'buf: 'headers> {
    Res(httparse::Response<'headers, 'buf>),
    Req(httparse::Request<'headers, 'buf>),
}

fn parse_http<'b, 'h>(http: &'b [u8], headers: &'h mut [httparse::Header<'b>]) -> Result<Http<'h, 'b>, String> {
    {
        let mut res = httparse::Response::new(headers);
        if let Ok(_) = res.parse(&http) {
            return Ok(Http::Res(res));
        }
    }

    {
        let mut req = httparse::Request::new(headers);
        if let Ok(_) = req.parse(&http) {
            return Ok(Http::Req(req));
        }
    }

    Err("Failed to parse HTTP headers".into())
}

fn main() {
    let buf = b"GET /index.html HTTP/1.1\r\nHost: example.domain\r\n\r\n";
    let mut headers = [httparse::EMPTY_HEADER; 16];

    match parse_http(buf, &mut headers) {
        Ok(http) => {
            match http {
                Http::Res(_) => println!("response"),
                Http::Req(_) => println!("request"),
            }
        }
        Err(e) => eprintln!("{}", e),
    }
}

This works, reusing the same borrow all the way through: Rust Playground

There might be a nicer way of doing it, not sure. You’re passing ownership of the sole mutable borrow to the response, so I think it stops existing once the response is dropped. I may be entirely wrong in my understanding of that though.

Edit: fixed playground link

Thanks a lot!

There's a (relatively) well-known issue of a (conditional) return extending the borrow to outside the function. Here's one github issue on it: https://github.com/rust-lang/rust/issues/40307

I thought NLL fixes this, but this playground suggests otherwise.

Maybe @pnkfelix can spare a few minutes to explain it.

I think NLL + Polonius will fix that issue. (More info about Polonius in this blog post: MIR-based borrow check (NLL) status update · baby steps )

1 Like

Thanks @krdln. For the lazy, the link there does say the following about the conditional returns:

1 Like