Issues importing local library crate

To start this thread - I recently started learning Rust. I am coming from Java, so well ... it is something different.

So currently I am trying to build an web app (for displaying the stuff I am retrieving from an API) and I am using leptos. So the 'lib part' of it I moved into another workspace and it compiles but I am not soo sure if I am not doing something wrong with the mod thingy in Rust. So I want to use the lib crate in another workspace from which a binary should be created.

It says 'use of undeclared crate or module 'xy''

So the general structure is the following:
/home/wuel/Development/Rust

β”œβ”€β”€ xy  (Library project)
    β”œβ”€β”€ Cargo.toml
    β”œβ”€β”€ Cargo.lock
    └── src
        β”œβ”€β”€ app
        β”‚   └── mod.rs        
        └── lib.rs            


└── xz  (Binary project)
    β”œβ”€β”€ Cargo.toml
    β”œβ”€β”€ Cargo.lock
    └── src
         └── main.rs          

/home/wuel/Development/Rust/craftlib/src/app/

app.rs

use leptos::*;
use leptos_dom::helpers::event_target_value;
use wasm_bindgen_futures::spawn_local;
use gloo_net::http::Request;
use leptos::prelude::signal;
use leptos::prelude::Get;
use leptos::prelude::Set;
use leptos::prelude::ElementChild;
use leptos::prelude::ClassAttribute;
use leptos::prelude::OnAttribute;  
    
#[component]
pub fn App() -> impl IntoView {
    // ZustΓ€nde fΓΌr Eingabe und Ausgabe
    let (sheet, set_sheet) = signal("Item".to_string());
    let (fields, set_fields) = signal("ID,Name".to_string());
    let (response, set_response) = signal("".to_string());
    
    let fetch_data = move |_| {
        let sheet_value = sheet.get();
        let fields_value = fields.get();
        
        spawn_local(async move {
            let url = format!(
                "https://v2.xivapi.com/api/sheet/{}?fields={}",
                sheet_value, fields_value
            );
            
            // Using gloo-net instead of reqwest
            match Request::get(&url).send().await {
                Ok(resp) => {
                    let text = resp.text().await.unwrap_or_else(|_| "Fehler beim Abrufen".to_string());
                    set_response.set(text);
                }
                Err(_) => set_response.set("Fehler beim Abrufen".to_string()),
            }
        });
    };
    
    view! {
        <div class="container">
            <h1>"XIVAPI Tester"</h1>
            
            <label>"Sheet Name:"</label>
            <input type="text"
                value={move || sheet.get()}
                on:input=move |e| {
                    set_sheet.set(event_target_value(&e))
                } />
            
            <label>"Fields (optional):"</label>
            <input type="text"
                value={move || fields.get()}
                on:input=move |e| {
                    set_fields.set(event_target_value(&e))
                } />
            <button on:click=fetch_data>"Abrufen"</button>
            <pre>{ move || response.get() }</pre>
        </div>
    }
}

mod.rs:

pub mod app;

Crago.toml (craftlib):

[package]
name = "craftlib"
version = "0.1.0"
edition = "2021"

[lib]
path = "src/app/app.rs"
crate-type = ["cdylib"]

[dependencies]
mio = { version = "1.0.3" }
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
wasm-bindgen-futures = "0.4"
leptos = { version = "0.7.8", features = ["csr"] }
gloo-net = "0.6.0"

/home/wuel/Development/Rust/crafting/src/

use leptos::*;
use leptos::mount::mount_to_body;
use craftlib::app::App; 

 
fn main() {
    mount_to_body(|| view! { <App/> });
}

Cargo.toml (crafting):

[package]
name = "crafting"
version = "0.1.0"
edition = "2021"

[package.metadata.leptos]
bin-package = "crafting" 
lib-package = "craftlib"
style-file = "style/main.css"
assets-dir = "assets"


[dependencies]
craftlib = { path = "/home/wuel/Development/Rust/craftlib" }
mio = { version = "1.0.3" }
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
wasm-bindgen-futures = "0.4"
leptos = { version = "0.7.8", features = ["csr"] }
gloo-net = "0.6.0"

[[bin]]
name = "crafting"
path = "src/main.rs"

Output on cargo build:

wuel@localhost:~/Development/Rust/crafting> cargo build
   Compiling crafting v0.1.0 (/home/wuel/Development/Rust/crafting)
error[E0433]: failed to resolve: use of undeclared crate or module `craftlib`
 --> src/main.rs:3:5
  |
3 | use craftlib::app::App;
  |     ^^^^^^^^ use of undeclared crate or module `craftlib`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `crafting` (bin "crafting") due to 1 previous error
wuel@localhost:~/Development/Rust/crafting>

'

When I do cargo build after a clean it also say Compiling craftlib before the Compiling of crafting fails.
I already spent some hours of trying stuff but I have no clue really - I wanted not to make an own crate for it but well leptos forced me a bit since a project can't be the bin and lib at the same time.
Thanks in advance :slight_smile:

You're referring to β€œxy” in some places and β€œcraftlib” in some other places. I suspect you have edited your code samples before posting. Exact naming is crucial to fixing this problem, so please make sure you are posting an exact copy of what you are trying to build β€” if necessary to protect secrets, create a separate test project that demonstrates the problem. Also, please post the entire output from cargo check or cargo build, not only one line.

Also, see Forum Code Formatting and Syntax Highlighting for how to get properly formatted code blocks on this forum.

2 Likes

Additionally, there also seems to be a misunderstanding concerning Cargo workspaces and packages, as I don't see a workspace anywhere in your example, just two packages xy and xz. Here the respective entries from the Cargo glossary:

1 Like

Thank you for pointing that out - I edited the post to make it clearer

I suspect your issue is that you can't import cdylibs as regular dependencies in other packages, maybe try and remove the crate-type = ["cdylib"] line?

Or if you need both, use this:

crate-type = ["lib", "cdylib"]

But it is often the case that libraries set up to be used as cdylibs are not ideal for use as Rust libraries (e.g. exporting unnecessary symbols) and it might be wise to split the library into two: one plain Rust library containing the actually shared code, and one dedicated to the purpose for which you need a cdylib.

2 Likes

That worked like what I needed - thanks <3. Now I just need to mess around a bit with leptos

Did I still got it wrong after editing my post? Or maybe my understanding is just a bit wrong

You moved the lib part to another package, not another workspace. A workspace is a collection of packages and a package is a set of crates (binaries, tests, examples and optionally a single library crate). AFAICT from your snippets, you don't have a workspace set up that would span the crafting and craftlib packages.

And you had to create the craftlib package, because Leptos seemingly doesn't allow both your binary crate and your library crate to exist within the same package. Other than that, the nomenclature looks fine to me. Before it became clear that crate-type is the issue, I thought the problem might've been related to your workspace (or lack of workspace) setup, which is why I raised the concern that you might be mixing concepts.

1 Like