-2

my "main" file

mod router;
mod student;

use std::sync::Arc;

use crate::router::init_router;
use crate::router::Memory;

use actix_web::{App, HttpServer};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    dotenv::dotenv().ok();
    let repo = Arc::new(Memory::repository());
    let host = std::env::var("SERVER.HOST").unwrap_or("127.0.0.1".to_string());
    let port = std::env::var("SERVER.PORT").unwrap_or("8080".to_string());
    let url = format!("{}:{}", host, port);
    println!("url: {}", &url);
    
    HttpServer::new(move || {
        App::new()
            .data(repo.clone())     // shared
            .configure(init_router)
    })
    .bind(&url)?
    .run()
    .await
}

my file: "router.rs"

use std::sync::Arc;

use crate::student::Student;
use actix_web::{get, web, HttpResponse, Responder};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct Memory {
    pub students: Vec<Student>,
}

impl Memory {
    fn new() -> Self {
        Memory {
            students: Vec::new(),
        }
    }

    pub fn repository() -> Self{
        Self {
            students: vec![
                {Student::new("1".to_string(), "Daniel".to_string(), 19)},
                {Student::new("2".to_string(), "Lucia".to_string(), 17)},
                {Student::new("3".to_string(), "Juan".to_string(), 14)}
            ]
        }
    }
}

#[get("/student/list/all")]
async fn list_all(repo: web::Data<Arc<Memory>>) -> impl Responder {
    HttpResponse::Ok().json(&***repo)
}

#[get("/student/list/by-id/{id_student}")]
async fn list_by_id(web::Path(id_student): web::Path<String>, repo: web::Data<Arc<Memory>>) -> impl Responder {
    HttpResponse::Ok().json(&***repo.students.into_iter().find(|x| *x.id == id_student))
}

pub fn init_router(config: &mut web::ServiceConfig) {
    config.service(list_all);
    config.service(list_by_id);
}

and my file: "student.rs"

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
pub struct Student{
    pub id: String,
    pub nombre: String,
    pub calificacion: u32,
}

impl Student{
    pub fn new(id: String, nombre: String, calificacion: u32) -> Self{
        Self{id, nombre, calificacion}
    }
}

I want to show a student in the following path: 127.0.0.1:3000/student/list/by-id/1 but i have the following error

error[E0614]: type `std::vec::IntoIter<Student>` cannot be dereferenced
  --> src\router.rs:43:33
   |
43 |     HttpResponse::Ok().json((&***repo.lock().unwrap().students.into_iter()).find(|x| *x.id == id_student))
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

because I get the error, I don't know what is wrong. Please I need help I am new to this programming language.

Daniel
  • 181
  • 4
  • 13
  • 1
    The error message is clearly pointing at the line the compiler has a problem with (even the exact expression within that line). What exactly don't you understand about what it's telling you? Why did you write that expression that way in the first place? – JMAA Mar 23 '21 at 20:17
  • (PS, your error message will also be more readable if you paste it into a code block rather than a block quote) – JMAA Mar 23 '21 at 20:19
  • @JMAA it does not allow me to run the program and I want to find a student from a list of students. – Daniel Mar 23 '21 at 20:25
  • I was trying to help you understand the problem yourself. I'm confident you're more than capable of working this out yourself logically, but just pasting a whole program and asking "fix this for me" is not going to make you a good programmer. – JMAA Mar 23 '21 at 20:28

1 Answers1

1

The dot operating will smartly dereference the pointer, so the following will compile:

&repo.students.iter().find(|x| *x.id == id_student)

the Arc will be dereferenced when accessing students from repo, which will give a reference to the Vec, and .iter() will give you a non-consuming iterator, which you can then map over (.into_iter() will require the Vec to be copied and consumed)

transistor
  • 1,480
  • 1
  • 9
  • 12
  • Thank you very much for your support, but because it did not work for me with ´into_iter ()´ – Daniel Mar 23 '21 at 20:38