How to use another package in my package?

Issue: Bring in Other packages/crates into scope

Synopsis

I have encountered some problems when I tried to bring in other packages.

In a nutshell, I'm wondering how to use another binary crate in the current binary crate.

Description

The structure of my project wordle is like this:

. is the current directory wordle.

.
├── Cargo.lock
├── Cargo.toml
├── Developer's Handbook.md
├── Doc.md
├── HONOR-CODE.md
├── README.md
├── f_package
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── solver
│   ├── Cargo.toml
│   └── src
│       └── main.rs
├── src
│   ├── builtin_words
│   │   └── mod.rs
│   ├── file_operation.rs
│   ├── lib.rs
│   ├── main.rs
│   ├── parse.rs
│   └── tests.rs
└── target

File: ./src/f_package/src/lib.rs

fn f_func1() {}

pub mod f_mod1 {
  pub struct FStruct1 {}
}

pub struct FStruct2 {}

In ./src/solver/src/main.rs

fn main() {
  // snip
}

pub mod solver_mod1 {
  pub mod solver_mod2 {
    pub struct SolverStruct1 {}
  }
}

File: ./Cargo.toml

I have added the two packages (or crates?) into the dependencies:

[dependencies]
f_package = { path = "./f_package" }
solver = { path = "./solver" }

File: ./src/lib.rs

Problems

Problem No.1

First, I want use the f_package in my project worlde. I managed to do this by adding use f_package in the file ./src/main.rs or in the file ./src/lib.rs

File : ./src/main.rs

Here , I want to use ./f_package, I have to add use::f_package; , then I can call all the modules and structs and functions in f_package

use f_package::{self,f_mod1};
// mod f_package
fn main() {
  // snip
  f_mod1::FStruct1{};
  f_package::Struct2{};
  f_package::f_func1();
}

Why I cannot use mod f_package / mod f_package::f_mod1 to bring the f_package/f_mod1 mod into the crate in my project wordle?

Problem No.2

File: ./src/main.rs

// snip
use solver; // this line generates an error

fn main() {
  // snip
  solver::solver_mod1{};// the compiler cannot file the module
}

Why can't I bring in the package solver like f_package ?

Problem No.3

Now, I declared a struct Game and a mod miniwordle in ./src/lib.rs

File: ./src/lib.rs

// snip
pub struct Game {
  // snip
}

pub mod miniwordle {
  // snip
}

Why do I have to use use wordle::Game to refer to the struct Game in the library crate ?

Actually, there are two crates in my project wordle : one generated by lib.rs, the other generated by main.rs. If I want to use an item in another crate, I have to use wordle:: to tell the compiler to find a crate tree in my project and look for the item I want ? Does the compiler know which crate I am referring to if there are two crates in one project ?

Then, how can I use another mod in lib.rs ? Why can't I use mod statement to bring that crate in lib.rs into the crate generated by main.rs ?

Because mod declares a new module (in the current crate), rather than referring to an existing module.

Because solver is a binary crate, so it isn't designed to be linked into other crates. If you want to use a crate as both a binary and library crate, then having both main.rs and lib.rs like in your wordle crate, is the right approach.

Because otherwise there is no way to distinguish a use of something in the binary crate and something in the library crate. When writing code in the binary crate, crate refers to the binary crate and wordle (or whatever the crate's name is) refers to the library crate.

You would use wordle for the library crate as described above. A crate can only access items in other library crates. Since each package can only have one library crate (and any number of binary crates), this is unambiguous.

Modules can be brought into scope using use. As mentioned above, mod declares a new module rather than bringing existing ones into scope.

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.