ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

rust async

2020-12-25 12:05:19  阅读:614  来源: 互联网

标签:-- futures edition Compiling error async rust


 

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
}

 

标签:--,futures,edition,Compiling,error,async,rust
来源: https://www.cnblogs.com/dream397/p/14188196.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有