2

I am trying to write a Rust library with this layout:

.
├── Cargo.lock
├── Cargo.toml
├── examples
│   └── main.rs
├── src
│   ├── client.rs
│   └── lib.rs
└── target
    └── rls
        └── debug
            ├── build
            ├── deps

client.rs has all the code and the client struct is public

lib.rs contains pub mod client;

examples/main.rs:

extern crate fistrs;

use fistrs::client::FistClient;

fn main() {
    let mut client = FistClient::new("localhost", "5575");
    client.connect();
}

but I get an error when I run this rustc examples/main.rs

 --> examples/main.rs:1:1
  |
1 | extern crate fistrs;
  | ^^^^^^^^^^^^^^^^^^^^ can't find crate

Here is my Cargo.toml

[package]
name = "fistrs"
version = "0.1.0"
authors = ["palash25 <npalash25@gmail.com>"]
edition = "2018"

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

[dependencies]

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Palash Nigam
  • 1,653
  • 3
  • 14
  • 26
  • It's hard to answer your question because it doesn't include a [MRE]. You need to provide the Cargo.toml as well as the contents of the library. Please remove **everything** from the library that does not contribute to the error; it's very likely that your library will be a single line when you've done this completely! There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Sep 30 '19 at 14:54
  • I don't understand the link between your question title and its body, could you please explain? – Kyll Sep 30 '19 at 14:57
  • @PaulStenne note that I changed the title in an attempt to make it clearer — is it better or worse now? – Shepmaster Sep 30 '19 at 14:58
  • @Shepmaster Oh. Uh. Not sure? I don't really understand the intent between the first title neither. Maybe it's an auto-completion issue? – Kyll Sep 30 '19 at 15:03
  • 1
    @PaulStenne my interpretation of the question suggests that the OP has created a library crate and wants to use that library in the Cargo examples. However, they are getting an error, so they want to figure out how to use the library "outside" of the `src` directory (that's my logical leap). I changed the title to match what is in the body: that they cannot use it in an example. – Shepmaster Sep 30 '19 at 15:06
  • @Shepmaster Well you're more of an expert than I. Lemmy, could you please explain us what you are trying to achieve? – Kyll Sep 30 '19 at 15:07
  • What's in your `Cargo.toml`? And what command do you run to get the error? – Jmb Sep 30 '19 at 15:08
  • Apologies for the incomplete post, I have updated the description – Palash Nigam Sep 30 '19 at 15:13

1 Answers1

5

The idiomatic answer

when I run this rustc examples/main.rs

Don't do that. Use either cargo run --example main or cargo build --example main instead.

See also:

The literal answer

When you build an example, there are two crates involved:

  1. The library crate
  2. The example crate

You would need to build the library crate, then inform the compiler about that crate when building the example crate. This is extremely tedious work that no one generally wants to do. It would look something like:

$ rustc --edition=2018 --crate-type=rlib --crate-name library_example src/lib.rs -o libmy_library.rlib
$ rustc --edition=2018 --extern library_example=libmy_library.rlib examples/main.rs

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366