"Cannot move out of dereference of" Error

Hi everybody. I'm trying to have it so that my server can properly respond to post requests from the client. I'm getting "Cannot move out of dereference of" Errors at info.key, info.field, and info.value. How do I go about fixing this? Code would be appreciated!

mod database;
use actix_files as fs;
use actix_web::{App, HttpResponse, HttpServer, Responder, web};
use serde::Deserialize;

#[derive(Deserialize)]
struct Info
{
    operation: String,
    key: String,
    field: String,
    value: String
}

async fn index(info: web::Json<Info>) -> impl Responder
{
    if info.operation == "set".to_string()
    {
        database::set(info.key, info.value).ok();
        HttpResponse::Ok().body("Success!")
    }
    else if info.operation == "get".to_string()
    {
        let data = (database::get(info.key).ok()).unwrap();
        HttpResponse::Ok().body(data)
    }
    else if info.operation == "hset".to_string()
    {
        database::hset(info.key, info.field, info.value).ok();
        HttpResponse::Ok().body("Success!")
    }
    else if info.operation == "hget".to_string()
    {
        let data = database::hget(info.key, info.field).ok().unwrap();
        HttpResponse::Ok().body(data)
    }
    else
    {
        HttpResponse::Ok().body("Unknown Operation")
    }
}

To just fix the error: add let info = info.into_inner(); as the first line of the index function.

The problem is that the Json wrapper does not allow you to move fields out of the Info it contains. By writing info.key and passing it to a function like database::set that I assume (based on the error message you describe) takes a String as argument, the compiler tries access the String value out of info by dereferencing but also needs that String by value which means it would need to move it out of the field of the Json-wrapped Info.

1 Like

Thank you it works!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.