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
  • Code of
pub fn print_test(){
  • Created
  • Code of
mod test_hello;
fn main(){

And everything worked. I get "hello!".
5. Now i wanted to use the library i cloned.
6. In src/ there is a
pub struct SecretKey
that im trying to access.
7. in my, 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 and, 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 from 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 or 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 or in, but not from both. If a file is needed from both, mention it only in and use a use statement to import it from

1 Like

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

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

First few lines of my Cargo.toml are:

name = "blst"
version = "0.3.10"

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

error[E0433]: failed to resolve: maybe a missing crate `blst`?
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 one. Did you intend to use the one on 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 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:
  8. in my src/ 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`?
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 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.