Rust `unresolved import` on third party library
Asked Answered
K

1

5

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?

Krueger answered 4/8, 2019 at 22:6 Comment(2)
What happens if you remove self? Note that it's not really needed anyways, since it's already in scope (at least in Rust 2018).Stutsman
If you don't want to specify extern crate you need to use the 2018 edition and add edition = "2018" to your Cargo.toml.Louvre
M
6

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.

Mccann answered 4/8, 2019 at 23:36 Comment(1)
wow, that was it :) with no other changes, it worked! Thanks!Krueger

© 2022 - 2025 — McMap. All rights reserved.