Rust 第三方库上的“未解决的导入”

问题描述 投票:0回答:1

我想使用名为 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

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

import module rust rust-cargo rust-crates
1个回答
5
投票

您不小心将 Cargo 项目设置为模拟旧的“2015”语言版本的向后兼容模式。

为了能够使用正常的 Rust 语法,请添加:

edition = "2021"

前往您的

Cargo.toml
[package]
部分。

开始新项目时,请始终使用

cargo new
。它将确保正确设置最新版本标志。

© www.soinside.com 2019 - 2024. All rights reserved.