Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
986 views
in Technique[技术] by (71.8m points)

rust - Unable to find symbols from extern crates included with `use`

I'm trying to use some Rust libraries from crates on Github. This is the first time I've tried to do this. The code, lifted from an "html" library example, begins like this:

mod interactive_test {
    extern crate http;
    extern crate url;
    use std::os;
    use std::str;
    use url::Url;

    use http::client::RequestWriter;
    use http::method::Get;
    use http::headers::HeaderEnum;
    // ...
}

fn main() {}

Errors look like this:

error[E0432]: unresolved import `url::Url`
 --> src/main.rs:7:9
  |
7 |     use url::Url;
  |         ^^^^^^^^ Did you mean `self::url`?

error[E0432]: unresolved import `http::client::RequestWriter`
 --> src/main.rs:9:9
  |
9 |     use http::client::RequestWriter;
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean `interactive_test::http`?

error[E0432]: unresolved import `http::method::Get`
  --> src/main.rs:10:9
   |
10 |     use http::method::Get;
   |         ^^^^^^^^^^^^^^^^^ Did you mean `self::http::method`?

error[E0432]: unresolved import `http::headers::HeaderEnum`
  --> src/main.rs:11:9
   |
11 |     use http::headers::HeaderEnum;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean `interactive_test::http`?

The Cargo.toml file contains

[dependencies.http]
http = "https://github.com/chris-morgan/rust-http"

[dependencies.url]
url = "0.2.7"

and the HTTP and URL packages were found and fetched by cargo build earlier.

The extern crate http and extern crate url lines do not generate errors; the crates are being found by the compiler, but those crates don't seem to contain the expected symbols. If I add `extern crate foo", I get an error, so that is checked.

This is probably some problem with how Rust or Cargo search for libraries. Rust is installed in ~/local, not as root, done by setting the --prefix parameter during installation. That may have broken something, although Cargo should handle that. Basic stuff like "hello_world" works fine; bringing in external libraries does not.

I notice that cargo update doesn't cause a re-fetch of the http and url crates from Github. The documentation indicates that it should.

Versions:

  • Ubuntu 14.04 LTS.
  • rustc 0.13.0-nightly (96a3c7c6a 2014-12-23 22:21:10 +0000)
  • cargo 0.0.1-pre-nightly (e11c317 2014-12-21 20:43:45 +0000)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The compiler gave you the answer you need.

Your extern crate statements are inside a module, and use statements require absolute paths. That is, when you say use url::Url; inside the interactive_test module, what you are actually saying is "use url::Url which is defined in the root module", which it isn't.

What you need to do is prefix the path with self:: to tell it to look in the current module. You can also use super:: to access the parent module (if that ever comes up).

Personally, I get around this by putting all my extern crate statements in the root module, which also serves as a kind of program-wide list of external crates being used.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...