出现 "error[E0433]: failed to resolve: could not find main in tokio" 错误通常是由于项目的结构或依赖关系配置不正确导致的。在 Rust 中,#[tokio::main] 宏用于标识应用程序的入口点,但它必须与正确配置的依赖库一起使用。在这篇文章中,我们将详细介绍如何解决这个问题,并解释 tokio 在 Rust 异步编程中的重要性。
1.异步 Rust 和 async/await
2.错误原因
3.解决方案
1. 确保依赖项正确配置
2. 创建异步的 main 函数
3.重新构建项目
4.运行项目
1.异步 Rust 和 async/await
在异步 Rust 编程中,我们经常使用 async/await 语法来处理非阻塞的异步任务。为了正确执行异步代码,你需要一个异步运行时,它负责管理异步任务的调度和执行。
tokio 是 Rust 中最常见的异步运行时之一,用于编写异步应用程序。在使用 tokio 时,通常会创建一个异步的 main 函数,以便启动异步运行时并运行异步任务。问题的根本原因可能在于你的项目中缺少了这个异步 main 函数。
2.错误原因
错误信息 failed to resolve: could not find mainintokio 表明编译器在 tokio 库中找不到 main 函数。在 Rust 中,程序的入口点是 main 函数,它是程序执行的起始点。通常,我们使用 #[tokio::main] 注解来标记 main 函数,以便在异步上下文中运行 Rust 程序。
3.解决方案
为了解决这个问题,你可以采
文章目录方法一、
tokio
+ reqwest + futures方法二、async-std + surf + futures
代码来自于 【原子之音】 视频,感谢up主.
主要测试了三个模块:
tokio
surf reqwest
surf【使用文档】
tokio
【使用文档】
reqwest【使用文档】
方法一、
tokio
+ reqwest + futures
[dependencies]
reqwest = {version="0.10.10",feature=["json"]}
tokio
= {v
第一步:这是最开始的bug
Error
:
Failed
to
resolve
: com.android.support:support-annotations:27.0.1
Install Repository and sync project
Open Fi
在我们到目前为止看到的例子
中
,我们用#[
tokio
::
main
]标记了主函数,并使整个项目成为异步的。然而,这对所有项目来说都是不可取的。例如,一个GUI应用程序可能希望在主线程上运行GUI代码,并在另一个线程上运行
Tokio
运行时。
本页解释了如何将异步/等待隔离到项目的一小部分。
#[
tokio
::
main
]扩展到什么?
#[
tokio
::
main
]宏是一个...
对于新手
Rust
er来说,最绝望的就是crate找不到了,use of undeclared crate or module和maybe a missing crate?
use of undeclared crate or module
在workspace方式组织的项目
中
,遇到了
error
[
E0433
]:
failed
to
resolve
: use of undeclared crate or module db_schema
–>
main
/src/
main
.rs:2:9
.carg 目录下 新建 空 Cargo.toml 后缀名是toml 类型的文件 就
解决
了
F:\
rust
>
rust
c rus.rs //生成多个文件并合并成exe文件
F:\
rust
> rus.exe // 执行
输出 ...
运行程序找不到库,或者叫依赖(
rust
里有crates, package, module,看字面理解吧)这里0.8.5是指版本,跟你的
rust
的版本有关。按照官网学
rust
,程序地址在这里,rand="0.8.5" ,如下,
继续并对示例 20-12
中
的 src∕
main
.rs 做出修改,并利用来自 cargo check 的编译器
错误
来驱动
开发
。下面是我们得到的第一个
错误
:ThreadPoolThreadPoolhello好的,这告诉我们需要一个 ThreadPool 类型或模块,所以我们将构建一个。ThreadPool 的实现会与web server 的特定工作相独立,所以让我们从 hello crate 切换到存放 ThreadPool 实现的新库 crate。