无法从包含在 `use` 中的 extern crate 中找到符号 [英] Unable to find symbols from extern crates included with `use`

查看:18
本文介绍了无法从包含在 `use` 中的 extern crate 中找到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Github 上 crate 中的一些 Rust 库.这是我第一次尝试这样做.从html"库示例中提取的代码如下所示:

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() {}

错误看起来像这样:

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`?

Cargo.toml 文件包含

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

[dependencies.url]
url = "0.2.7"

并且之前的 cargo build 发现并获取了 HTTP 和 URL 包.

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

extern crate httpextern crate url 行不会产生错误;编译器正在找到这些板条箱,但这些板条箱似乎不包含预期的符号.如果我添加 `extern crate foo",我会收到一个错误,所以会被检查.

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.

这可能是 Rust 或 Cargo 如何搜索库的一些问题.Rust 安装在 ~/local 中,而不是以 root 身份安装,通过在安装过程中设置 --prefix 参数来完成.这可能破坏了某些东西,尽管 Cargo 应该处理它.像hello_world"这样的基本东西工作正常;引入外部库不会.

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.

我注意到 cargo update 不会导致从 Github 重新获取 http 和 url 包.文档表明它应该.

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.

版本:

  • 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)

推荐答案

编译器给了你需要的答案.

The compiler gave you the answer you need.

您的extern crate 语句inside 一个模块,而use 语句需要绝对 路径.也就是说,当你在 interactive_test 模块中说 use url::Url; 时,你实际上是在说use url::Url这是在根模块中定义的",它不是.

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.

您需要做的是在路径前面加上 self:: 以告诉它在当前模块中查找.您还可以使用 super:: 访问父模块(如果有的话).

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).

就我个人而言,我通过将我所有的 extern crate 语句放在根模块中来解决这个问题,根模块也作为一种程序范围内正在使用的外部 crate 列表.

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.

这篇关于无法从包含在 `use` 中的 extern crate 中找到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆