首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >找不到tokio::主宏?

找不到tokio::主宏?
EN

Stack Overflow用户
提问于 2020-09-13 18:27:01
回答 2查看 15.4K关注 0票数 21

我正在我的Windows系统中创建一个示例Rust项目,以异步模式通过HTTP请求下载一个文件。

我的代码如下(与锈蚀食谱中提到的代码相同):

代码语言:javascript
运行
复制
extern crate error_chain;
extern crate tempfile;
extern crate tokio;
extern crate reqwest;

use error_chain::error_chain;
use std::io::copy;
use std::fs::File;
use tempfile::Builder;

error_chain! {
     foreign_links {
         Io(std::io::Error);
         HttpRequest(reqwest::Error);
     }
}

#[tokio::main]
async fn main() -> Result<()> {
    let tmp_dir = Builder::new().prefix("example").tempdir()?;
    let target = "https://www.rust-lang.org/logos/rust-logo-512x512.png";
    let response = reqwest::get(target).await?;

    let mut dest = {
        let fname = response
            .url()
            .path_segments()
            .and_then(|segments| segments.last())
            .and_then(|name| if name.is_empty() { None } else { Some(name) })
            .unwrap_or("tmp.bin");

        println!("file to download: '{}'", fname);
        let fname = tmp_dir.path().join(fname);
        println!("will be located under: '{:?}'", fname);
        File::create(fname)?
    };
    let content =  response.text().await?;
    copy(&mut content.as_bytes(), &mut dest)?;
    Ok(())
}

我的Cargo.toml文件是:

代码语言:javascript
运行
复制
[package]
name = "abcdef"
version = "0.1.0"
authors = ["xyz"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
error-chain = "0.12.4"
tempfile = "3.1.0"
tokio = "0.2.22"
reqwest = "0.10.8"

当我执行cargo run时,会显示以下错误:

代码语言:javascript
运行
复制
error[E0433]: failed to resolve: could not find `main` in `tokio`
  --> src\main.rs:18:10
   |
18 | #[tokio::main]
   |          ^^^^ could not find `main` in `tokio`

error[E0277]: `main` has invalid return type `impl std::future::Future`
  --> src\main.rs:19:20
   |
19 | async fn main() -> Result<()> {
   |                    ^^^^^^^^^^ `main` can only return types that implement `
std::process::Termination`
   |
   = help: consider using `()`, or a `Result`

error[E0752]: `main` function is not allowed to be `async`
  --> src\main.rs:19:1
   |
19 | async fn main() -> Result<()> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

我交叉检查了Cargo.toml文件& edition = "2018"已经在那里了。我找不出其他的错误。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-09-13 18:31:08

您需要在tokio中启用一个额外的特性,以便能够使用tokio::main

尝试将full特性添加到Cargo.toml文件中的tokio依赖项中:

代码语言:javascript
运行
复制
[dependencies]
tokio = { version = "0.2.22", features = ["full"] }

这也适用于较晚版本的Tokio。

票数 33
EN

Stack Overflow用户

发布于 2021-04-07 12:35:51

tokio::main文档中所述

仅可在rtmacros和机箱功能上使用。

您需要添加这些特性才能访问tokio::main

代码语言:javascript
运行
复制
[dependencies]
tokio = { version = "1", features = ["rt", "macros"] }

然而,这将只允许访问单线程执行器,因此您必须使用#[tokio::main(flavor = "current_thread")]。如果要使用#[tokio::main] (与#[tokio::main(flavor = "multi_thread")]相同),则需要启用多线程执行器

仅可在rt-multi-thread机箱功能上使用。

代码语言:javascript
运行
复制
[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }

另请参阅:

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63874178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档