cargo check
[root@bogon async]# cargo check Checking own v0.1.0 (/data2/rust/async) error[E0670]: `async fn` is not permitted in the 2015 edition --> src/main.rs:6:1 | 6 | async fn hello_world() { | ^^^^^ to use `async fn`, switch to Rust 2018 | = help: set `edition = "2018"` in `Cargo.toml` = note: for more on editions, read https://doc.rust-lang.org/edition-guide error[E0433]: failed to resolve: maybe a missing crate `futures`? --> src/main.rs:4:5 | 4 | use futures::executor::block_on; | ^^^^^^^ maybe a missing crate `futures`? error[E0425]: cannot find function `block_on` in this scope --> src/main.rs:12:5 | 12 | block_on(future); // `future` is run and "hello, world!" is printed | ^^^^^^^^ not found in this scope error: aborting due to 3 previous errors Some errors have detailed explanations: E0425, E0433, E0670. For more information about an error, try `rustc --explain E0425`. error: could not compile `own`. To learn more, run the command again with --verbose. [root@bogon async]#
cargo fmt
[root@bogon async]# cat Cargo.toml [package] name = "own" version = "0.1.0" authors = ["Karthikeyan kartmuhil@gmail.com"] [dependencies] [root@bogon async]# cargo fmt error[E0670]: `async fn` is not permitted in the 2015 edition --> /data2/rust/async/src/main.rs:6:1 | 6 | async fn hello_world() { | ^^^^^ to use `async fn`, switch to Rust 2018 | = help: set `edition = "2018"` in `Cargo.toml` = note: for more on editions, read https://doc.rust-lang.org/edition-guide [root@bogon async]# cat Cargo.toml [package] name = "own" version = "0.1.0" authors = ["Karthikeyan kartmuhil@gmail.com"] [dependencies]
cargo build Compiling own v0.1.0 (/data2/rust/async) error[E0670]: `async fn` is not permitted in the 2015 edition --> src/main.rs:6:1 | 6 | async fn hello_world() { | ^^^^^ to use `async fn`, switch to Rust 2018 | = help: set `edition = "2018"` in `Cargo.toml` = note: for more on editions, read https://doc.rust-lang.org/edition-guide error[E0433]: failed to resolve: maybe a missing crate `futures`? --> src/main.rs:4:5 | 4 | use futures::executor::block_on; | ^^^^^^^ maybe a missing crate `futures`? error[E0425]: cannot find function `block_on` in this scope --> src/main.rs:12:5 | 12 | block_on(future); // `future` is run and "hello, world!" is printed | ^^^^^^^^ not found in this scope error: aborting due to 3 previous errors Some errors have detailed explanations: E0425, E0433, E0670. For more information about an error, try `rustc --explain E0425`. error: could not compile `own`. To learn more, run the command again with --verbose.
rustc -Vv rustc 1.47.0 (18bf6b4f0 2020-10-07) binary: rustc commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39 commit-date: 2020-10-07 host: aarch64-unknown-linux-gnu release: 1.47.0 LLVM version: 11.0
我们要手工给项目对应的Cargo.toml添加edition = "2018"
,试试cargo build
,现在应该妥了。
cat Cargo.toml
[package]
name = "own"
version = "0.1.0"
authors = ["Karthikeyan kartmuhil@gmail.com"]
edition = "2018"
[dependencies]
cargo build Compiling own v0.1.0 (/data2/rust/async) error[E0433]: failed to resolve: use of undeclared type or module `futures` --> src/main.rs:4:5 | 4 | use futures::executor::block_on; | ^^^^^^^ use of undeclared type or module `futures` error[E0425]: cannot find function `block_on` in this scope --> src/main.rs:12:5 | 12 | block_on(future); // `future` is run and "hello, world!" is printed | ^^^^^^^^ not found in this scope error: aborting due to 2 previous errors Some errors have detailed explanations: E0425, E0433. For more information about an error, try `rustc --explain E0425`. error: could not compile `own`. To learn more, run the command again with --verbose.
https://rust-lang.github.io/async-book/01_getting_started/04_async_await_primer.html
[dependencies] futures = "0.3"
cat Cargo.toml [package] name = "own" version = "0.1.0" authors = ["Karthikeyan kartmuhil@gmail.com"] edition = "2018" [dependencies] futures = "0.3"
cargo build Updating crates.io index Compiling proc-macro2 v1.0.24 Compiling unicode-xid v0.2.1 Compiling syn v1.0.55 Compiling proc-macro-hack v0.5.19 Compiling memchr v2.3.4 Compiling proc-macro-nested v0.1.6 Compiling futures-sink v0.3.8 Compiling futures-core v0.3.8 Compiling once_cell v1.5.2 Compiling futures-io v0.3.8 Compiling pin-utils v0.1.0 Compiling slab v0.4.2 Compiling futures-channel v0.3.8 Compiling futures-task v0.3.8 Compiling quote v1.0.8 Compiling pin-project-internal v1.0.2 Compiling futures-macro v0.3.8 Compiling pin-project v1.0.2 Compiling futures-util v0.3.8 Compiling futures-executor v0.3.8 Compiling futures v0.3.8 Compiling own v0.1.0 (/data2/rust/async) Finished dev [unoptimized + debuginfo] target(s) in 40.35s [root@bogon async]# cargo build Finished dev [unoptimized + debuginfo] target(s) in 0.03s [root@bogon async]# cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.03s Running `target/debug/own` hello, world!
// `block_on` blocks the current thread until the provided future has run to // completion. Other executors provide more complex behavior, like scheudling // multiple futures onto the same thread. use futures::executor::block_on; async fn hello_world() { println!("hello, world!"); } fn main() { let future = hello_world(); // Nothing is printed block_on(future); // `future` is run and "hello, world!" is printed }