HiveBrain v1.2.0
Get Started
← Back to all entries
debugrustMajor

Cannot find tokio::main macro?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
findmacromaintokiocannot

Problem

I am creating a sample Rust project in my Windows system to download a file by HTTP GET request in async mode.

My code is as follows (same as the code mentioned in the Rust Cookbook):
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(())
}



My Cargo.toml file is:

[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"


When I execute cargo run, the following errors are displayed:
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 retu

Solution

You need to enable an extra feature in tokio to be able to use tokio::main.

Try adding the full feature to the tokio dependency in your Cargo.toml file:

[dependencies]
tokio = { version = "0.2.22", features = ["full"] }


This also applies to later versions of Tokio.

Code Snippets

[dependencies]
tokio = { version = "0.2.22", features = ["full"] }

Context

Stack Overflow Q#63874178, score: 78

Revisions (0)

No revisions yet.