我有这个模块:
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/