RUST 在 web::Data<Arc<>> 中找到值

RUST find value in web::Data<Arc<>>

我的 “主要” 文件

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
}

我的文件:"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);
}

和我的文件:"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}
    }
}

我想在以下路径中显示学生:127.0.0.1:3000/student/list/by-id/1 但我有以下错误

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))
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

错误:由于先前的错误而中止

因为报错,不知道哪里出了问题。我需要帮助,我是这种编程语言的新手。

点操作会巧妙地取消对指针的引用,因此以下内容将编译:

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

当从 repo 访问 students 时,Arc 将被取消引用,这将引用 Vec,而 .iter() 将为您提供一个非消耗性迭代器,然后您可以将其映射到 (.into_iter() 将要求 Vec 被复制和消费)