Cannot call functions from other file

Hello, im quite new to the rust programming, and im not 100 % sure what im doing..

I will explain in steps what i did:

  1. git clone GitHub - supranational/blst: Multilingual BLS12-381 signature library
  2. changed my directory to bindings/rust
  3. opened vs code in directory in step 2
  4. to make sure things worked i did this:
  • Created test_hello.rs
  • Code of test_hello.rs:
pub fn print_test(){
    println!("{}","hello!");
}
  • Created main.rs
  • Code of main.rs
mod test_hello;
fn main(){
    test_hello::print_test();
}

And everything worked. I get "hello!".
5. Now i wanted to use the library i cloned.
6. In src/lib.rs there is a
pub struct SecretKey
that im trying to access.
7. in my main.rs, i added line of code
mod lib;
8. When i tried running the code, i get 60 errors. And im not sure why...
9. How can i access the SecretKey struct and call required functions i need?

If you have both a main.rs and lib.rs, then your project is actually two crates: the binary and library crate. The mod statement is used to define a new module of the current crate, so its not appropriate for accessing things from another crate. The correct way to access things in lib.rs from main.rs is by doing

use my_crate_name::ThingInLibRs;

Here, my_crate_name is the name at the top of your Cargo.toml.

As a more general point, if you ever do any of the following, then you've made a mistake:

  1. Mention main.rs or lib.rs using a mod statement.
  2. Mention any specific file with a mod statement more than once.

Note in particular that files in src/ may be mentioned with a mod in main.rs or in lib.rs, but not from both. If a file is needed from both, mention it only in lib.rs and use a use statement to import it from main.rs.

2 Likes

Thanks for the reply, i think we are getting somewhere.
So, i am trying to follow this documentation https://crates.io/crates/blst too.
In USAGE section, there it says to use for example use blst::min_pk::*
Now my main.rs in src looks like this:

use blst::min_pk::*;
fn main(){
}

First few lines of my Cargo.toml are:

[package]
name = "blst"
version = "0.3.10"

it also have includes, readme, etc...
I tried running my main.rs and im getting:

error[E0433]: failed to resolve: maybe a missing crate `blst`?
 --> main.rs:1:5
  |
1 | use blst::min_pk::*;
  |     ^^^^ maybe a missing crate `blst`?
  |
  = help: consider adding `extern crate blst` to use the `blst` crate

i also tried adding line:use blst::* but it didnt help...

You're naming your own library the same as the crates.io one. Did you intend to use the one on crates.io instead?

It looks like you are trying to use blst in your own project. This isn't the way to do it.

What you should do is create your own project, in its own directory (separate from the blst directory tree), with its own Cargo.toml and its own name. Then you can add blst = "0.3.10" to the dependencies section of your Cargo.toml (as mentioned at the bottom of the crates.io page, under "Install"), or even easier do cargo add blst in your terminal. Then you can add the use statement in your files and access the items in the blst crate.

If you haven't already, you should take the time to read the book. The use of the rand crate in the guessing game example from chapter 2 shows adding a crate as a dependency, similar to what you want to do here.

Okay, i understand.
This is what i did:

  1. Created new Folder: BLST
  2. opened VSCode in BLST
  3. opened terminal
  4. cargo new projX
  5. cd projX/
  6. cargo add blst
  7. checked documentation here: https://crates.io/crates/blst/0.3.10
  8. in my src/main.rs i added line of code use blst::min_pk::* as provided in step 7.
  9. tried running the code.
  10. Getting error:
error[E0433]: failed to resolve: maybe a missing crate `blst`?
 --> main.rs:1:5
  |
1 | use blst::min_pk::*;
  |     ^^^^ maybe a missing crate `blst`?
  |
  = help: consider adding `extern crate blst` to use the `blst` crate

Im really sorry for this newbie mistakes... :confused:

Well, i tried to do that inside the cloned project...so i think thats why it didnt work...

Okay, how are you running the code? Inside VSCode? What happens if you do cargo run or cargo build from a terminal? What is the full output?

Okay... i was running it diffrently. cargo run solved!

1 Like

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.