Search code examples
importmodulerustrust-cargorust-crates

Rust `unresolved import` on third party library


I want to compile a simple rust program using a third party library named warp:

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

[dependencies]
warp = "0.1.18"

In 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));
}

When I run cargo build I see it download warp and lots of transitive dependencies, then I get the errors:

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

I've gone through various docs on modules and crates. What am I doing wrong in this simple scenario?


Solution

  • You've accidentally set your Cargo project to backwards-compatible mode that emulates an old "2015" version of the language.

    To be able to use normal Rust syntax, add:

    edition = "2021"
    

    to your Cargo.toml's [package] section.

    When starting new projects, always use cargo new. It will ensure the latest edition flag is set correctly.