Rust 为什么我会得到;恐慌于';目前未在东京运行时运行'&引用;使用期货板条箱上的block_on时?

Rust 为什么我会得到;恐慌于';目前未在东京运行时运行'&引用;使用期货板条箱上的block_on时?,rust,rust-tokio,Rust,Rust Tokio,我正在使用elastic search的博客文章中关于他们的新板条箱的示例代码,但我无法使其按预期工作。线程因线程“main”在“Tokio运行时当前未运行”而崩溃。 什么是东京运行时,我如何配置它,为什么我必须 use futures::executor::block_on; async elastic_search_example() -> Result<(), Box<dyn Error>> { let index_response = client

我正在使用elastic search的博客文章中关于他们的新板条箱的示例代码,但我无法使其按预期工作。线程因线程“main”在“Tokio运行时当前未运行”而崩溃。

什么是东京运行时,我如何配置它,为什么我必须

use futures::executor::block_on;

async elastic_search_example() -> Result<(), Box<dyn Error>> {
    let index_response = client
        .index(IndexParts::IndexId("tweets", "1"))
        .body(json!({
            "user": "kimchy",
            "post_date": "2009-11-15T00:00:00Z",
            "message": "Trying out Elasticsearch, so far so good?"
        }))
        .refresh(Refresh::WaitFor)
        .send()
        .await?;
    if !index_response.status_code().is_success() {
        panic!("indexing document failed")
    }
    let index_response = client
        .index(IndexParts::IndexId("tweets", "2"))
        .body(json!({
            "user": "forloop",
            "post_date": "2020-01-08T00:00:00Z",
            "message": "Indexing with the rust client, yeah!"
        }))
        .refresh(Refresh::WaitFor)
        .send()
        .await?;
    if !index_response.status_code().is_success() {
        panic!("indexing document failed")
    }
}

fn main() {
    block_on(elastic_search_example());
}
use futures::executor::block_on;
异步弹性搜索示例()->结果{
让index_response=client
.index(indexpart::IndexId(“tweets”,“1”))
.body(json({
“用户”:“kimchy”,
“发布日期”:“2009-11-15T00:00:00Z”,
“消息”:“尝试Elasticsearch,到目前为止还不错?”
}))
.refresh(refresh::WaitFor)
.send()
.等待?;
如果!index\u response.status\u code()为\u success(){
恐慌!(“索引文档失败”)
}
让index_response=client
.index(indexpart::IndexId(“tweets”,“2”))
.body(json({
“用户”:“forloop”,
“发布日期”:“2020-01-08T00:00:00Z”,
“消息”:“与rust客户端建立索引,耶!”
}))
.refresh(refresh::WaitFor)
.send()
.等待?;
如果!index\u response.status\u code()为\u success(){
恐慌!(“索引文档失败”)
}
}
fn main(){
block_on(弹性搜索示例());
}

看起来Elasticsearch的板条箱在内部使用,所以您也必须使用它来匹配他们的假设

在他们的文档中查找函数的
block\u,我已经找到了。因此,您的
main
看起来应该是这样的:

use tokio::runtime::Runtime;

fn main() {
    Runtime::new()
        .expect("Failed to create Tokio runtime")
        .block_on(elastic_search_example());
}
或者您可以使
main
函数本身与异步,这将生成运行时创建和
block\u on
调用:

#[tokio::main]
async fn main() {
    elastic_search_example().await;
}

当我用内部使用
tokio02
(tokio版本=0.2)的板条箱运行
tokio::run
(从tokio版本=0.1)时,我也遇到了同样的错误(在我的例子中,它是reqwest)。 首先,我刚刚将std::future::future
更改为
futures01::future::future
。让它编译。跑步后,我完全明白你的错误

解决方案: 添加解决了我的问题



从哪里导入
block_on
函数?它来自
futures::executor::block_on
。这澄清了很多问题。谢谢。使用未编译的第二种方法,它会产生以下两个错误:错误[E0277]:
main
的返回类型无效
impl futures::Future
-->src\main.rs:110:17 | 110 | async fn main(){^ ^ <代码>主< <代码>只能返回实现<代码>终止< /代码> =帮助:考虑使用<代码>(代码)>或<代码>结果< /代码>错误[E072]:<代码>主< <代码>函数不允许为<代码>异步 > -SRC\Maul.Rs:110∶110×异步FN主(){| ^^^^^^^^^^ ^
main
函数不允许是
async
请用您的实际代码和实际错误提出新问题,因为您使用的代码与示例中的代码不完全相同,或者没有显示您得到的完整错误。