I'm working on building a REST API using Rust and Rocket. I have an endpoint at which I create a new user, defined as follows:
/// View with which to create a user
#[post("/users", format = "application/json", data = "<user_data>")]
fn create_user(user_data: Json<UserData>, db: DB) -> Status<Json<Value>> {
let conn = db.conn();
let _new_user_result = user_data.into_new_user(&conn);
unimplemented!()
}
Note that there is no borrowed content here; both user_data
and db
are owned. Still, I get the following error on compilation:
error[E0507]: cannot move out of borrowed content
--> src/views/user_account.rs:75:28
|
75 | let _new_user_result = user_data.into_new_user(&conn);
| ^^^^^^^^^ cannot move out of borrowed content
For reference, the function signature of into_new_user
is
fn into_new_user(self, conn: &SqliteConnection) -> Result<NewUser, Status<Json<Value>>> {
...
}
What is going on here? This error would be much easier to understand if I were actually borrowing anything, but given that I own everything in question, I'm baffled.
$ rustc --version; cargo --version
rustc 1.22.0-nightly (a47c9f870 2017-10-11)
cargo 0.23.0-nightly (e447ac7e9 2017-09-27)
user_data
is not an ownedUserData
object. It's aJson<UserData>
object, whereJson<T>
dereferences toT
. It happens thatJson<T>
is only used for its constructor, but the compiler doesn't know that. Now I just need to figure out how to solve this, and I'll write it up as a solution. – coriolinus