【问题标题】:Getting "use of undeclared type or module" when accessing impl module访问 impl 模块时获取“使用未声明的类型或模块”
【发布时间】:2021-01-25 10:02:08
【问题描述】:

我有这个模块:

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::admin_index 具有什么样的价值?没有参数的函数?一个参数的函数?未来? impl Responder?
  • 此时不应该有任何问题。这里的问题是编译器声称 basicAdapters 是未声明的,尽管它的声明很明显。

标签: rust


【解决方案1】:

:: 是命名空间解析运算符,basicAdapters 不是命名空间。

要对值调用方法,请使用. 运算符:

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

【讨论】:

  • 你是对的。太感谢了。我首先没有考虑您的解决方案的原因是编译器没有抱怨该方法,而是抱怨“basicAdapters”结构。至少,这就是我理解错误信息的方式。再次感谢!
猜你喜欢
  • 2017-02-18
  • 2021-01-14
  • 2014-06-19
  • 2015-08-08
  • 1970-01-01
  • 2019-11-13
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多