Search code examples
rustnickel

cannot borrow `*request` as mutable because it is also borrowed as immutable


I've written an endpoint in Rust using nickel.rs:

{
    let client = client.clone();
    let mongodb = MongoRepository::new(client, "rust-users".into());
    router.put("/users/:id",middleware!(|request, mut response| {

        let id = try_with!(response, { 
            request.param("id")
                   .ok_or((StatusCode::BadRequest, "Id parameter required")) 
        });
        let user = try_with!(response, {
            request.json_as::<User>()
                   .map_err(|e| 
                       (StatusCode::BadRequest, format!("{}",e))
                   )
        });
        let update_id = try_with!(response, {
            mongodb.update_by_id("users", id, user)
                   .map_err(|e| 
                       (StatusCode::InternalServerError, e) 
                   )
        });
        ApiResult::Ok(StatusCode::Ok, update_id.to_json())

    }));
}

In this line:

let id = try_with!(response, { 
    request.param("id")
           .ok_or((StatusCode::BadRequest, "Id parameter required")) 
});

request is borrowed in an immutable state, but in this line:

let user = try_with!(response, {
    request.json_as::<User>()
           .map_err(|e| 
               (StatusCode::BadRequest, format!("{}",e))
           )
});

request is borrowed in a mutable state. This causes the following error:

error: cannot borrow `*request` as mutable because it is also borrowed
as immutable

I tried to fix this error by wrapping the id line in a separate scope but the issue persisted.

I'm not sure how else to fix the issue. Is there something else that's wrong with the code?


Solution

  • id is of type &str. This is a pointer into the variable request. Calling json_as takes as a parameter a mutable reference to the variable request. This leads to the error "cannot borrow *request as mutable because it is also borrowed as immutable".

    There are a number of ways you could solve this; probably the most straightforward is to use to_owned() to convert the &str into a String.