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" }