import - 第三方库上的 Rust `unresolved import`

标签 import module rust rust-cargo rust-crates

我想使用名为 warp 的第三方库编译一个简单的 Rust 程序:

[package]
name = "hello-world-warp"
version = "0.1.0"

[dependencies]
warp = "0.1.18"

src/main.rs 中:

use warp::{self, path, Filter};

fn main() {
    // GET /hello/warp => 200 OK with body "Hello, warp!"
    let hello = warp::path!("hello" / String)
        .map(|name| format!("Hello, {}!", name));

    warp::serve(hello)
        .run(([127, 0, 0, 1], 3030));
}

当我运行 cargo build 时,我看到它下载了 warp 和许多传递依赖项,然后我得到了错误:

Compiling hello-world-warp v0.1.0 (<path>) error[E0432]: unresolved import `warp`
 --> src/main.rs:3:12
  |
3 | use warp::{self, path, Filter};
  |            ^^^^ no `warp` in the root

error: cannot find macro `path!` in this scope

我浏览了有关模块和箱子的各种文档。在这个简单的场景中我做错了什么?

最佳答案

您复制的示例使用的语法适用于最新版本的 Rust,但您不小心将 Rust 设置为模拟旧的“2015”版本的语言。

您必须添加:

edition = "2018"

到您的 Cargo.toml[package] 部分。

开始新项目时,始终使用cargo new。它将确保正确设置最新版本标志。

关于import - 第三方库上的 Rust `unresolved import`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57350520/

相关文章:

Python - 使用 tablib 导入 Excel(xls、xlsx)文件

rust - 在调用 HashMap::get 时是否可以使用任何构造来启用对 HashMap 的变异?

ruby - 是否将字符串传回 FFI 模块中必要的调用代码以防止内存泄漏?

rust - 如何调用返回多种类型的不同函数指针的 C 函数?

import - 如何在 Jsfiddle 中使用 Angular2 和 Typescript

ubuntu - 在 ubuntu 中导入 tensorflow 时出错

import - Cython cimport 和 __init__.pyx 有问题吗?

python - 无法重新加载位于另一个文件夹中但也在 sys.path 中的模块

c++ - 如何将 C++ 程序分解为模块

javascript - Typescript 1.6 命名空间和导入类 (commonJS)