代码之家  ›  专栏  ›  技术社区  ›  clay

信任第三方库上的“未解决的导入”。新手问题

  •  0
  • clay  · 技术社区  · 6 年前

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

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

    0 回复  |  直到 6 年前
        1
  •  1
  •   Kornel    6 年前

    您复制的示例使用的语法适用于最新版本的rust,但您意外地设置了rust来模仿旧的“2015”版本的语言。

    必须添加:

    edition = "2018"
    

    给你的 Cargo.toml [package] 第节。

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