0
我使用nickel.rs寫在魯斯特的端點:不能借用`* request`爲可變,因爲它是還借爲不可改變
{
let client = client.clone();
let mongodb = MongoRepository::new(client,"rust-users".into());
router.put("https://stackoverflow.com/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())
}));
}
在這一行:
let id = try_with!(response,{request.param("id").ok_or((StatusCode::BadRequest, "Id parameter required")) });
request
是以不可變的狀態借來的,但在這一行中:
let user = try_with!(response,{request.json_as::<User>()
.map_err(|e| (StatusCode::BadRequest, format!("{}",e)))});
請求是在一個可變的sta TE。這會導致以下錯誤:
error: cannot borrow
*request
as mutable because it is also borrowed as immutable
我試圖通過包裝在一個單獨的範圍id
線來解決這個錯誤,但問題依然存在。
我不知道如何解決這個問題。代碼中有其他問題嗎?
通過試驗和錯誤我終於弄清楚了我的錯誤。感謝您的支持:) – menawi