4

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?

cafce25
  • 15,907
  • 4
  • 25
  • 31
clay
  • 18,138
  • 28
  • 107
  • 192
  • 1
    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). – jhpratt Aug 04 '19 at 22:26
  • 2
    If you don't want to specify `extern crate` you need to use the 2018 edition and add `edition = "2018"` to your `Cargo.toml`. – mcarton Aug 04 '19 at 22:41

1 Answers1

4

The example you copied uses a syntax that works in the most recent edition of Rust, but you've accidentally set your Rust to emulate an old "2015" version of the language.

You must add:

edition = "2018"

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.

Kornel
  • 97,764
  • 37
  • 219
  • 309