[rust-dev] Using an external crate from a module

Ben Foppa benjamin.foppa at gmail.com
Mon Nov 3 10:48:15 PST 2014


I don't know whether it's *discouraged*, but you're right that things get
confusing quickly. So far, keeping all the `mod`s and `extern crate`s in a
separate file (a la
https://github.com/bfops/playform/blob/339667691197ec02afd2fee55d080e541723b12e/src/playform.rs)
hasn't given me any issues, so I haven't had a reason not to default to
that.

On Sun, Nov 2, 2014 at 9:22 AM, David Henningsson <diwic at ubuntu.com> wrote:

> Hi,
>
> I'm wondering if "external crate" declarations from modules are
> discouraged in general? Because things seem to become a bit quirky, took me
> a while to grasp:
>
> First, "use" starts at the root whereas everything else starts at the
> current module. E g, imagine a "test_seri.rs" file which looks like this:
>
> extern crate serialize;
>
> fn test_json() {
>     use self::serialize::json;
>     let _:int = json::decode("").unwrap();
> }
>
> fn test_json2() {
>     let _:int = serialize::json::decode("").unwrap();
> }
>
> So, since the "serialize" crate is inserted into the test_seri namespace,
> I have to do "use self::serialize::json" and not "use serialize::json" in
> the example above. But still I can call "serialize::json::decode" in the
> second example without using the entire path "self::serialize::json" (or
> test_seri::serialize::json). This is a bit inconsistent, but doable once
> you understand it.
>
> Second, in the case of the log crate, it looks like it can only be
> imported in the root crate, not in a module. For two reasons. Imagine a "
> test_log.rs" file looking like this:
>
> #![feature(phase)]
> #[phase(plugin, link)] extern crate log;
>
> fn test_log() {
>     error!("Yo!");
> }
>
> The first is that "#![feature(phase)]" seems to be silently ignored when
> not in crate root. You'll get a somewhat confusing compiler error on the
> second row: "add #![feature(phase)] to the crate attributes to enable" -
> it's confusing until you get that you have added "#![feature(phase)]" to a
> module, not a crate.
>
> The second is that the log macros ("error!" etc) reference
> ::log::LogLocation, which assumes that the log is actually imported as
> ::log. Which it isn't in this case, it's test_log::log. Again a confusing
> compiler error "error: failed to resolve. Maybe a missing `extern crate
> log`?".
> (Side note: the macros are also broken if you do stuff such as 'extern
> crate "log" as foo'.)
>
> // David
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20141103/5f3f6ef2/attachment.html>


More information about the Rust-dev mailing list