user266003
user266003

Reputation:

A crate is exported but "use" throws compilation errors

In Cargo I have this:

postgres = "0.11.7"

And in a submodule *.rs

extern crate postgres;

use postgres::{Connection, Error, FromSql, SslMode};
use postgres::Result as PgResult;

And the compilation errors:

error: unresolved import `postgres::Connection`. Did you mean `self::postgres`? [E0432]
unresolved import `postgres::Error`. Did you mean `self::postgres`? [E0432]

error: unresolved import `postgres::SslMode`. Did you mean `self::postgres`? [E0432]

And the similar ones.

Upvotes: 0

Views: 571

Answers (1)

Vladimir Matveev
Vladimir Matveev

Reputation: 128051

While it is possible to put extern crate directives to any module, it is both more idiomatic and more convenient to put it to your crate root, usually lib.rs or main.rs. Then your use statements will work as they are now.

The reason for this problem is that you have put extern crate postgres to one of submodules of the root crate:

mod submodule {
    extern crate postgres;

    use postgres::...;
}

This means that the full path to postgres module would be submodule::postgres, not just postgres (remember, paths in use directives are by default absolute), so you should use either use submodule::postgres::whatever; or use self::postgres::whatever; (the latter form is an example of relative paths in use statement). However, as I said before, this is not idiomatic and should be avoided.

Upvotes: 3

Related Questions