Trouble using first rust library

Hello,

I am experimenting with library creation, and I am having some odd problems.

I have my library in one folder, and my binary in the other.
Here is the directory structure:
image

I am trying to use this new library in my main function, but I am getting this error:

error[E0432]: unresolved import `test_library::it_works`
 --> src\main.rs:2:5
  |
2 | use test_library::it_works;
  |     ^^^^^^^^^^^^^^^^^^^^^^ no `it_works` in the root

error: aborting due to previous error

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

To learn more, run the command again with --verbose.

Here is the main.rs file:

extern crate test_library;
use test_library::it_works;

fn main() {

    println!("Hello, world!");

    it_works();

}

However, taking a look at my lib.rs file, this function is in fact in there (the root), it's just:

pub fn it_works() {

    assert_eq!(2 + 2, 4);

    println!("It works!");

}

I did add the test_library to my cargo.toml file under the bins dependencies:

[dependencies]
test_library = "0.1.0"

I'm not getting it, can someone tell me what I missed?

I am having a similar problem in the library when I add in my prelude file into the lib.rs file like this:

pub mod prelude;
pub fn it_works() {

    assert_eq!(2 + 2, 4);

    println!("It works!");

}

I get about the same error, but for the prelude file:

error[E0432]: unresolved import `super::astruct`
 --> src\prelude.rs:1:16
  |
1 | pub use crate::astruct::ThatThing;
  |                ^^^^^^^ maybe a missing crate `astruct`?

error: aborting due to previous error

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

To learn more, run the command again with --verbose.

Here is my prelude.rs file, which is in the same src folder as lib.rs:

pub use crate::astruct::ThatThing;

And here is the other file, called astruct.rs which is also in the same src folder as lib.rs:

pub struct ThatThing {
    pub anum : i64
} 

Please let me know what I need to fix; I'm new to libraries, and I can't find a good explanation.
I did read the help that rustc gives, but it didn't really clarify the solution.

Thanks!

For the second problem add this line to lib.rs:

mod astruct;

This will create the astruct module at the top level of your crate, and populate it with the contents of the astruct.rs file.

1 Like

Thanks!

I thought that adding in prelude would get me what ever prelude refers to?? I suppose not?

In terms of the first of 2 issues, the error:

error[E0432]: unresolved import `test_library::it_works`
 --> src\main.rs:2:5
  |
2 | use test_library::it_works;
  |     ^^^^^^^^^^^^^^^^^^^^^^ no `it_works` in the root

error: aborting due to previous error

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

To learn more, run the command again with --verbose.

Still doesn't make sense.

Here is the lib.rs file again:

pub mod prelude;

mod astruct;

pub fn it_works() {

    assert_eq!(2 + 2, 4);

    println!("It works!");

}

Do you know why it is throwing that error when I try to build the binary?

Thanks!

For the first problem, the test_library line in the [dependencies] section of Cargo.toml is telling Cargo to download the test_library crate from crates.io. You should remove this line.

1 Like

Ok, but when I remove that from the cargo.toml file, it no longer recognizes it at all:

does the extern need to go?

How do I get it to recognize the private library then?

Thanks!

You always need to use the mod keyword to create each module of your crate. The use keyword will import names from a module that was created elsewhere (in the same crate or a differnet one), but it will never create a module on its own.

By default, lib.rs is compiled into a library with the same name as your Cargo project. It looks like your project is named library_test, so you should change this:

use test_library::it_works;

to this:

use library_test::it_works;

Or, if you want the library name to be different from the project name, you can add a [lib] section to Cargo.toml:

[lib]
name = "test_library"

(Side note: You can also remove the extern crate line completely if you have edition = "2018" in your Cargo.toml.)

Hmm, that didn't work. In this case, the bin and the library are two separate "projects", they are both in the same main folder, but I have them in there own sub folders, which I created separately using cargo.
image
So the test_library is the name of the library "project"

Would I need to register the library so cargo knows where to find it? I'm still stumped on that one.

Thanks

Ah, in this case you can use a path dependency. In library_test/Cargo.toml:

[dependencies]
test_library = { path = "../test_library", version = "0.1.0" }

(Note: You don't need to include the version number here. It's useful if you plan to publish both crates on crates.io eventually, but otherwise it is ignored.)

1 Like

Success!

Ok, this is making a lot more sense, thanks for the help!

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.