3

I start with the cargo new tst. Then in the src/lib.rs I have:

pub struct Config {}

And src/main.rs looks like the following:

use crate::Config;

fn main() {}

This however does not compile:

> cargo run
   Compiling tst v0.1.0 (/home/*/rust/book/tst)
error[E0432]: unresolved import `crate::Config`
 --> src/main.rs:1:5
  |
1 | use crate::Config;
  |     ^^^^^^^^^^^^^ no `Config` in the root

For more information about this error, try `rustc --explain E0432`.
error: could not compile `tst` due to previous error

But if I replace crate:: with the name of the crate like so:

use tst::Config;

fn main() {}

Then it just work:

> cargo run
   Compiling tst v0.1.0 (/home/*/rust/book/tst)
warning: unused import: `tst::Config`
 --> src/main.rs:1:5
  |
1 | use tst::Config;
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `tst` (bin "tst") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.25s
     Running `target/debug/tst`

The output for rustc --explain E0432 has the following quote, which if I understand it correctly means, that I can either use the name of a crate or simply crate:::

In Rust 2018, paths in use statements are relative to the current module unless they begin with the name of a crate or a literal crate::, in which case they start from the crate root. As in Rust 2015 code, the self:: and super:: prefixes refer to the current and parent modules respectively.

Am I doing something wrong in here? Is there a way to use code from lib.rs without hardcoding the name of the crate?

> rustc --version
rustc 1.56.1 (Arch Linux rust 1:1.56.1-3)

> cat Cargo.toml
[package]
name = "tst"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
NarūnasK
  • 4,564
  • 8
  • 50
  • 76
  • Hello, is because main.rs and lib.rs in the same package is like two crates. See https://stackoverflow.com/questions/57756927/rust-modules-confusion-when-there-is-main-rs-and-lib-rs. – Zeppi Dec 13 '21 at 07:58
  • Does this answer your question? [Rust modules confusion when there is main.rs and lib.rs](https://stackoverflow.com/questions/57756927/rust-modules-confusion-when-there-is-main-rs-and-lib-rs) – orlp Dec 13 '21 at 08:11
  • @orlp it doesn't, you can see I know I can use crate's name if I want to `use` stuff from the `lib.rs`, however I'm asking if there's a way/workaround/hack/whatever not to hardcode the name of the `crate` in `main.rs`. Thanks! – NarūnasK Dec 13 '21 at 12:12

1 Answers1

0

There is no way to refer to “the library crate in my package”; you must refer to it by its name. The Rust compiler doesn't (currently) know anything about other crates that happen to be in the same Cargo package; it just compiles one crate at a time.

In order for this to change, you would need to write a Rust RFC, which would need to include a description of why it would be valuable to have this feature — probably a stronger argument than “it would be less hardcoded”, since main depending on the library is almost certainly going to use library-specific names anyway, so the name of the library itself is a minor issue (unless, I suppose, you're creating many packages from a common template).

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108