Simple Import Problem?

I am migrating a rust project to python and need to understand how a function is working. I know a little about rust but am hardly an expert

I simply want to be able to import log::warn into ab_data.rs so I can trace the logic.

The project structure is:

/src
  lib.rs
  /app
     main.rs
     ...
  /models/
     ab_data.rs
     ...

However, it's not simple. I've tried multiple ways to get ab_data.rs to import log,
and get messages like:

  |
9 | use log;
  |     ^^^ no `log` in the root

lib.rs

#![feature(plugin, custom_derive)]
#![plugin(rocket_codegen)]

#[macro_use]
extern crate log;

extern crate mysql;

extern crate pwhash;

extern crate chrono;

extern crate rand;

extern crate itertools;

...

app/main.rs

#![feature(plugin, custom_derive)]
#![plugin(rocket_codegen)]

#[macro_use]
extern crate log;

extern crate mysql;

extern crate pwhash;

extern crate chrono;

extern crate rand;

extern crate itertools;

...

app/models/ab_data.rs

use models::{Date, User};

use mysql;
use mysql::{Pool, Row};

use log;
use log::{warn};

...

impl AbData {
    /// Takes an input struct, and returns an output struct with all fields calculated.
    pub fn calculate(...) -> Self {
        warn!('warn message');
    ...

Cargo.toml

...


[[bin]]
...
path = "src/app/main.rs"

...

[dependencies]
...
log = "0.3.8"
mysql = "11.3"
...

[dev-dependencies]
assert_approx_eq = "1.0"

[lib]
path = "src/lib.rs"

Dockerfile


RUN rustup install nightly-2018-03-15
RUN rustup default nightly-2018-03-15

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . .
RUN cargo build --release

...

Note that the mysql import works in this module, and I believe I'm following the exact same pattern for log, but it cannot be located.

I'm not a rust programmer, as I mentioned. Any help is appreciated. Let me know if I missed anything important.

That use mysql; seems strange – you don't need to use an external crate. Previously, that's what extern crate foo; declarations were for, but they are no longer needed. You only have to use specific items, e.g. modules or functions or types. So in ab_data.rs, simply write use log::warn and omit use log;.

It's because OP uses really old version of the toolchain, specifically nightly-2018-03-15. Note that the rocket 0.5.0-rc1 published on the crates.io supports latest stable compiler.

TBH the project doesn't seems like to follow cargo's directory structure convention well, so it's hard to answer without full project code.

I changed the import to in ab_data.rs to:

use log::warn

All else is the same.

^[[200~Compiling oof v0.1.0 (file:///usr/src/app)
     Running `rustc --crate-name oof src/lib.rs --crate-type lib --emit=dep-info,link -C opt-level=3 -C metadata=a6d76a53cb48c138 -C extra-filename=-a6d76a53cb48c138 --out-dir /usr/src/app/target/release/deps -L dependency=/usr/src/app/target/release/deps --extern log=/usr/src/app/target/release/deps/liblog-4c2c27534399b1b6.rlib --extern itertools=/usr/src/app/target/release/deps/libitertools-061aac5f6a16697a.rlib --extern csv=/usr/src/app/target/release/deps/libcsv-5ace0e4688aa6b17.rlib --extern chrono=/usr/src/app/target/release/deps/libchrono-0f22aeb61358564b.rlib --extern serde_json=/usr/src/app/target/release/deps/libserde_json-0435b953fb213de0.rlib --extern rand=/usr/src/app/target/release/deps/librand-2731456ac85ebc28.rlib --extern rocket=/usr/src/app/target/release/deps/librocket-7502f9880f286565.rlib --extern mysql=/usr/src/app/target/release/deps/libmysql-eb4501a2bbe50b89.rlib --extern serde_derive=/usr/src/app/target/release/deps/libserde_derive-74c329f0867012e1.so --extern pwhash=/usr/src/app/target/release/deps/libpwhash-cc680bda9d304137.rlib --extern serde=/usr/src/app/target/release/deps/libserde-a49310ce8ce6e306.rlib --extern rocket_codegen=/usr/src/app/target/release/deps/librocket_codegen-01600a20f6530594.so -L native=/usr/src/app/target/release/build/rust-crypto-062cce10ac32465c/out -L native=/usr/src/app/target/release/build/ring-d67241b48f99fc8e/out`
error[E0432]: unresolved import `log::warn`
 --> src/models/ab_data.rs:8:5
  |
8 | use log::warn;
  |     ^^^^^^^^^ no `warn` in the root

error: cannot find macro `params!` in this scope
  --> src/models/users.rs:68:13
   |
68 |             params!{"id" => id,
   |             ^^^^^^

   |                 ^^^^^^

[note: multiple params errors - doesn't normally happen.]
error: aborting due to 17 previous errors

For more information about this error, try `rustc --explain E0432`.
error: Could not compile `oof`.

Caused by:
  process didn't exit successfully: `rustc --crate-name oof src/lib.rs --crate-type lib --emit=dep-info,link -C opt-level=3 -C metadata=a6d76a53cb48c138 -C extra-filename=-a6d76a53cb48c138 --out-dir /usr/src/app/target/release/deps -L dependency=/usr/src/app/target/release/deps --extern log=/usr/src/app/target/release/deps/liblog-4c2c27534399b1b6.rlib --extern itertools=/usr/src/app/target/release/deps/libitertools-061aac5f6a16697a.rlib --extern csv=/usr/src/app/target/release/deps/libcsv-5ace0e4688aa6b17.rlib --extern chrono=/usr/src/app/target/release/deps/libchrono-0f22aeb61358564b.rlib --extern serde_json=/usr/src/app/target/release/deps/libserde_json-0435b953fb213de0.rlib --extern rand=/usr/src/app/target/release/deps/librand-2731456ac85ebc28.rlib --extern rocket=/usr/src/app/target/release/deps/librocket-7502f9880f286565.rlib --extern mysql=/usr/src/app/target/release/deps/libmysql-eb4501a2bbe50b89.rlib --extern serde_derive=/usr/src/app/target/release/deps/libserde_derive-74c329f0867012e1.so --extern pwhash=/usr/src/app/target/release/deps/libpwhash-cc680bda9d304137.rlib --extern serde=/usr/src/app/target/release/deps/libserde-a49310ce8ce6e306.rlib --extern rocket_codegen=/usr/src/app/target/release/deps/librocket_codegen-01600a20f6530594.so -L native=/usr/src/app/target/release/build/rust-crypto-062cce10ac32465c/out -L native=/usr/src/app/target/release/build/ring-d67241b48f99fc8e/out` (exit code: 101)
The command '/bin/sh -c cargo build --release --verbose' returned a non-zero code: 10

I've found messing with the rocket version breaks other things, but I'll give it a shot.

I'm trying to retire this app.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.