DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+2 votes

I have a project that I want to divide into three parts: core, gui and web. So that I can compile it as either a gui or a web. I've created the base directory as mkdir jedi-archives and inside that I've done cargo new --lib core-lib, cargo new --bin gui and cargo new --bin web.

❯ tree
.
├── Cargo.lock
├── Cargo.toml
├── core-lib
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── gui
│   ├── Cargo.toml
│   └── src
│       └── main.rs
├── README.md
├── target
└── web
    ├── Cargo.toml
    ├── src
    │   ├── main.rs
    │   └── main_test.rs
    ├── static
    └── templates

9 directories, 10 files

Then I've added the function add-one to core-lib/src/lib.rs as shown in the docs. And I've tried using it from web/src/main.rs but I get an error:

cargo build
error: no matching package named `core-lib` found
location searched: /home/user/sync/code/rust/jedi_archives/core-lib
required by package `jedi-archives-web v0.0.0 (/home/user/sync/code/rust/jedi_archives/web)`

The code:

❯ cat core-lib/src/lib.rs
pub fn add_one(x: i32) -> i32 {
    x + 1
}

❯ cat web/src/main.rs
use core-lib;

#[macro_use] extern crate rocket;

#[get("/")]
fn index() -> &'static str {
    let mut num = 10;
    num = core-lib::add-one(num);
    "Hello, world! {num}"
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![index])
}

❯ cat Cargo.toml
[workspace]

members = [
    "core-lib",
    "web",
    "gui",
]

❯ cat core-lib/Cargo.toml
[package]
name = "jedi-archives-core-lib"
version = "0.0.0"
workspace = "../"
edition = "2021"
publish = false

[dependencies]

❯ cat web/Cargo.toml
[package]
name = "jedi-archives-web"
version = "0.0.0"
workspace = "../"
edition = "2021"
publish = false

[dependencies]
rocket = "0.5.0-rc.1"
core-lib = { path = "../core-lib" }
by
edited by
0

Can you also share the output of tree from your workspace directory? Your libraries should be created at the same level of the binary, but you said that you've created them inside the package folder. It's not clear if you've run the commands in the workspace directory or in the binary directory.

2 Answers

+1 vote
 
Best answer

I see a couple of issues:

  • use core-lib; should be use core_lib;. This means that you also need to change core-lib::add_one to core_lib::add_one
  • core-lib = { path = "../core-lib" } should be under [dependencies] and not at the top of the Cargo.toml file
  • remove jedi-archives- from the package names in the Cargo.toml files
by
edited by
0

Why use core_lib instead of use core-lib?

0

Because hyphens are disallowed when naming crates (but they are allowed when naming packages). You can find more info at RFC 940: hyphens_considered_harmful.

0

Did this actually solve your problem? I can't spot other issues. I've also compiled it and it woks for me.

0

No, I get the same error. I've changed core-lib for core_lib only in web/src/main.rs. Should I change it somewhere else?

0

See updated answer.

+1 vote

If for any reason your core-lib library must be called "jedi-archives-core-lib" and you cannot rename the package (perhaps it's already used by other crates?), another option is to rename the dependencies.

$ cat core-lib/Cargo.toml
[package]
name = "jedi-archives-core-lib"

$ cat web/Cargo.toml
[package]
name = "jedi-archives-web"

[dependencies]
rocket = "0.5.0-rc.1"
core-lib = { path = "../core-lib", package = "jedi-archives-core-lib" }
by
Contributions licensed under CC0
...