【发布时间】:2021-11-01 08:19:44
【问题描述】:
我在 Rust Actix 上实现 scylla-rust-driver 时遇到问题。我只想用 scyllaDb 创建简单的 CRUD。
首先我为应用数据创建结构
struct AppState {
scy_session: Session,
app_name: String,
}
接下来我创建简单函数
#[get("/")]
async fn index(data: web::Data<AppState>) -> String {
// I want to CRUD in this function with ScyllaDB
let app_name = &data.app_name; // <- get app_name
format!("Hello {}!", app_name) // <- response with app_name
}
最后 main.rs 是
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let session: Session = SessionBuilder::new()
.known_node("localhost:9042")
.user("username", "password")
.build()
.await
.expect("Some error message");
HttpServer::new(|| {
App::new()
.data(AppState {
scy_session: session,
app_name: String::from("Actix-web"),
})
.service(index)
})
.bind("127.0.0.1:8080")?
.run()
.await
}
但显示错误:
error[E0277]: the trait bound `Session: Clone` is not satisfied in `[closure@src/main.rs:27:21: 34:6]`
--> src/main.rs:27:5
|
27 | HttpServer::new(|| {
| _____^^^^^^^^^^^^^^^_-
| | |
| | within `[closure@src/main.rs:27:21: 34:6]`, the trait `Clone` is not implemented for `Session`
如何在 Actix web 上实际实现 scylla-rust-db ? 非常感谢
【问题讨论】:
-
根本没有测试,但是如果你在闭包中添加
move关键字呢?HttpServer::new(move || { -
仍然是同样的错误 @yolenoyer 先生 - 无论如何谢谢``` #[actix_web::main] | ^^^^^^^^^^^^^^^^^^ 在
[closure@src/main.rs:27:21: 34:10]内,Clone的特征没有为Session实现... 27 | HttpServer::new(move||{ | _____________________- 28 | | App::new() 29 | | .data(AppState { 30 | | scy_session: session, ```