Cannot borrow `response` as mutable more than once at a time

Hello, Already 4 hours that I fight with Rust but nothing makes it, he always refuses to do what I want. If you can help me here is the code and the error:

         response.header("Set-Cookie", "cookie1=test");
         response.status(StatusCode::OK);
         println!("{:?}",eq.method()); //GET
         let mut ctn = ContentString::new(eq);
         {
             let mut test = |string| {
                 response.header("Set-Cookie", "cookie2=test");
                 println!("{}",string);
             };
             let mut tester = |string| {
                 response.header("Set-Cookie", "cookie3=test");
                 println!("{}",string);
             };
             test("test for error");
             tester("second test off error");
         }
         ctn.echo("<html>");
         ctn.echo("<head>");
         ctn.echo("<title>Test WebSite</title>");
         ctn.echo("</head>");
         ctn.echo("<body>");
         ctn.echo("<h1>test</h1>");
         ctn.echo("</body>");
         ctn.echo("</html>");
         return response.body(Body::from(ctn.get().into_bytes())).unwrap();```

 <pre><font color="#EF2929"><b>error[E0499]</b></font><b>: cannot borrow `response` as mutable more than once at a time</b>
<font color="#729FCF"><b>--&gt; </b></font>src/main.rs:29:34
<font color="#729FCF"><b>|</b></font>
<font color="#729FCF"><b>25</b></font> <font color="#729FCF"><b>| </b></font>                let mut test = |string| {
<font color="#729FCF"><b>| </b></font>                               <font color="#729FCF"><b>--------</b></font> <font color="#729FCF"><b>first mutable borrow occurs here</b></font>
<font color="#729FCF"><b>26</b></font> <font color="#729FCF"><b>| </b></font>                    response.header(&quot;Set-Cookie&quot;, &quot;cookie2=test&quot;);
<font color="#729FCF"><b>| </b></font>                    <font color="#729FCF"><b>--------</b></font> <font color="#729FCF"><b>previous borrow occurs due to use of `response` in closure</b></font>
<font color="#729FCF"><b>...</b></font>
<font color="#729FCF"><b>29</b></font> <font color="#729FCF"><b>| </b></font>                let mut tester = |string| {
<font color="#729FCF"><b>| </b></font>                                 <font color="#EF2929"><b>^^^^^^^^</b></font> <font color="#EF2929"><b>second mutable borrow occurs here</b></font>
<font color="#729FCF"><b>30</b></font> <font color="#729FCF"><b>| </b></font>                    response.header(&quot;Set-Cookie&quot;, &quot;cookie3=test&quot;);
<font color="#729FCF"><b>| </b></font>                    <font color="#729FCF"><b>--------</b></font> <font color="#729FCF"><b>borrow occurs due to use of `response` in closure</b></font>
<font color="#729FCF"><b>...</b></font>
<font color="#729FCF"><b>35</b></font> <font color="#729FCF"><b>| </b></font>            }
<font color="#729FCF"><b>| </b></font>            <font color="#729FCF"><b>-</b></font> <font color="#729FCF"><b>first borrow ends here</b></font>

<font color="#EF2929"><b>error</b></font><b>: aborting due to previous error</b>

<b>For more information about this error, try `rustc --explain E0499`.</b>
<font color="#EF2929"><b>error:</b></font> Could not compile `web`.
</pre>

Thank's for your response.

Use the code formatting when you can, in order to make your example more readable:

```rust
  code
````

Back to your example: you are creating two closures, and both wants to take a mutable reference to response.
I see that this is a test code, so I am not sure what you would like to do with test and ŧester, but I'll show you a couple of options:

  1. Don't create the two closures. In this case (but maybe it is just for the example) you don't need them.
  2. Scope the closures. If test is dropped before creating tester, you are good to go. Something like
{
  let test = /* ... */;
  test(/* ... */);
}

{
  let tester = /* ... */;
  tester(/* ... */);
}
  1. You really need both closures available and both need a reference to response? You can try to encapsulate response in Rc<RefCell<>> or similar.

It is difficult to give you a more specific suggestion without more details. I hope these points could help you a bit.

Thank's you a lot!

Rc<RefCell<>>

works!
You're a God man!!!

You may not need the Rc - just a RefCell may suffice if the closures are executed in that block of code and don’t need to actually share ownership over the response. In that case, the closures would hold a &RefCell<...>.

1 Like

For this simple case you are absolutely right, but at the same time the situation can be resolved using proper scopes. I imagined a more general situation in which the closures are moved inside an object, and the Rc is needed to extend the lifetime accordingly.

Maybe we could be both wrong about the optimal solution, in the end we are only looking at a testing code :smile:

Yeah, I’m just throwing out another option - I don’t know what the optimal is because, as you say, there’s insufficient info.

Based on the example, one can also have the closures receive a mutable borrow of the response and add the header inside, rather than capturing the response. But, don’t know if that’s workable in the real code.

1 Like