rust - 访问 impl 模块时出现 "use of undeclared type or module"

标签 rust

我有这个模块:
src/adapters.rs

use super::db::{init_connection, models};
use actix_web::Responder;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::MysqlConnection;

pub struct Basic {
   pool: Pool<ConnectionManager<MysqlConnection>>,
}

impl Basic {

    pub fn new() -> Basic {
        Basic {
            pool: init_connection().unwrap(),
        }
    }

    pub async fn admin_index(&self) -> impl Responder {
        "API Admin"
    }
}
我想从模块中调用实例方法 admin_index :
src/routes.rs
像这样:
use actix_web::{web, HttpResponse, Responder};
use super::adapters::Basic;

pub fn create(app: &mut web::ServiceConfig) {

    let basicAdapters = Basic::new();

    app
        .service(web::resource("/").to(|| HttpResponse::Ok().body("index")))
        .service(
            web::scope("/api")
                .service(
                    bweb::scope("/admin")
                        .route("/", web::get().to(basicAdapters::admin_index))
                )
}
但我不断得到:
error[E0433]: failed to resolve: use of undeclared type or module `basicAdapters`

.route("/", web::get().to(basicAdapters::admin_index))
                          ^^^^^^^^^^^^^ use of undeclared type or module `basicAdapters`
我不明白为什么会收到此错误消息,因为 basicAdapters 显然是由
let basicAdapters = Basic::new();
任何帮助表示赞赏。

最佳答案

::是命名空间解析运算符,basicAdapters不是命名空间。
要对值调用方法,请使用 .运算符(operator):

web::get().to(basicAdapters.admin_index())

关于rust - 访问 impl 模块时出现 "use of undeclared type or module",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64295004/

相关文章:

vector - 包含另一个向量的向量的类型是什么?

vector - 如何在一行中连接不可变向量?

generics - 使用 AsRef 返回包含在输入包装器类型中的引用

rust - 为什么该特征没有实现?

pattern-matching - 为什么这个结构在模式匹配后没有 move ?

rust - 什么单一类型可以指超 HttpConnector 和 HttpsConnector?

iterator - 如何在 Rust 中返回盒装的可克隆迭代器?

rust - 从键盘获取 Y/N 响应

sockets - Rust并发读写导致的死锁问题?

path - 如何替换PathBuf或Path的文件扩展名?