Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 将池设置为结构类型会导致错误_Rust - Fatal编程技术网

Rust 将池设置为结构类型会导致错误

Rust 将池设置为结构类型会导致错误,rust,Rust,我无法将池设置为结构类型 use super::db::{init_connection, models}; actix_web::Responder; use diesel::r2d2::Pool; #[derive(Debug)] pub struct Basic { pool: Pool } impl Basic { pub fn new(&self) { self.pool = init_connection().unwind(

我无法将池设置为结构类型

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

#[derive(Debug)]        
pub struct Basic {
    pool: Pool
}

impl Basic {
    pub fn new(&self) {
        self.pool = init_connection().unwind(); 
    }
}
我一直在犯错误

pool: Pool
      ^^^^ expected 1 type argument

据我所知,“池”是一种类型。这里还需要什么?

Pool是Pool类型的通用结构,其中M表示要为特定于数据库的逻辑实现的特性,以创建数据库连接。 例如,MySQL、Postgres、sqlite、mongodb等都有单独的这种特性的实现。根据您想要使用的数据库,寻找合适的板条箱,如r2d2 mongodb、r2d2_sqlite等

对于Mysql,一种选择是使用r2d2_Mysql板条箱。这有一个结构MysqlConnectionManager,它实现了r2d2::ManageConnection特性。它可以按如下所示使用:

// Imports
use r2d2::Pool;
use r2d2_mysql::MysqlConnectionManager;

// Define data type for connection pool. This can be part of app state
pool: Arc<Pool<MysqlConnectionManager>>,

// Initialize connection pool
let manager = r2d2_mysql::MysqlConnectionManager::new(builder);
let pool = Arc::new(r2d2::Pool::new(manager).unwrap());
//导入
使用r2d2::Pool;
使用r2d2_mysql::MysqlConnectionManager;
//定义连接池的数据类型。这可能是应用程序状态的一部分
游泳池:Arc,
//初始化连接池
让manager=r2d2_mysql::MysqlConnectionManager::new(builder);
让pool=Arc::new(r2d2::pool::new(manager.unwrap());

请参阅此处的板条箱文档,例如:

Pool
需要一个类型参数:很抱歉回答得太晚。我知道游泳池是通用的。在我的例子中,我实际上使用了Mysql实现。但是M是,这很可能是我的问题,这个特质的名字是不是?其特点是柴油机::r2d2::泳池,对吗?所以我需要做的是:pub-struct-Basic{pool:}这对吗?我这样问是因为我也尝试过这个,我得到self.pool=init_connection();^^^未知领域我认为我的问题更根本,根源在于对这里发生的事情的误解。