找不到 tokio::main 宏?
Cannot find tokio::main macro?
我正在我的 Windows 系统中创建示例 Rust 项目,以在异步模式下通过 HTTP GET 请求下载文件。
我的代码如下(与Rust Cookbook中提到的the code相同):
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 文件是:
[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
时,出现如下错误:
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"
已经存在。我无法找出其他错误。
您需要在 tokio
中启用一项额外功能才能使用 tokio::main
。
尝试将 full
功能添加到 Cargo.toml 文件中的 tokio
依赖项:
[dependencies]
tokio = { version = "0.2.22", features = ["full"] }
这也适用于更高版本的 Tokio。
如 tokio::main
的文档所述:
Available on crate features rt
and macros
only.
您需要添加这些功能才能访问 tokio::main
:
[dependencies]
tokio = { version = "1", features = ["rt", "macros"] }
这将只允许访问 the single-threaded executor, however, so you'd have to use #[tokio::main(flavor = "current_thread")]
. If you want to use #[tokio::main]
(which is the same as #[tokio::main(flavor = "multi_thread")]
, then you need to enable the multi-threaded executor:
Available on crate feature rt-multi-thread
only.
[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
另请参阅:
我正在我的 Windows 系统中创建示例 Rust 项目,以在异步模式下通过 HTTP GET 请求下载文件。
我的代码如下(与Rust Cookbook中提到的the code相同):
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 文件是:
[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
时,出现如下错误:
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"
已经存在。我无法找出其他错误。
您需要在 tokio
中启用一项额外功能才能使用 tokio::main
。
尝试将 full
功能添加到 Cargo.toml 文件中的 tokio
依赖项:
[dependencies]
tokio = { version = "0.2.22", features = ["full"] }
这也适用于更高版本的 Tokio。
如 tokio::main
的文档所述:
Available on crate features
rt
andmacros
only.
您需要添加这些功能才能访问 tokio::main
:
[dependencies]
tokio = { version = "1", features = ["rt", "macros"] }
这将只允许访问 the single-threaded executor, however, so you'd have to use #[tokio::main(flavor = "current_thread")]
. If you want to use #[tokio::main]
(which is the same as #[tokio::main(flavor = "multi_thread")]
, then you need to enable the multi-threaded executor:
Available on crate feature
rt-multi-thread
only.
[dependencies]
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
另请参阅: