Use of undeclared crate or module with workspaces - rust

I have the following structure:
-- project/
|
|-- Cargo.toml
|-- Cargo.lock
|-- src/
|
|-- main.rs
|-- crate1/
|-- lib.rs
|-- Cargo.toml
|-- tests
|-- Cargo.toml
|-- test.rs
and this are the content of the Cargo.toml
# Cargo.toml
...
[workspace]
members = [
"crate1",
"tests"
]
...
# crate1/Cargo.toml
[package]
name = "crate1"
...
[lib]
path = "lib.rs"
...
here I'm using another lib for my tests, I don't think the problem is here, because I already used this way to do my tests several times, and it worked perfectly, but for some reason, now this is happening to me, I don't know if everything is a typo error of my self or not, but I already checked it a lot of times
# tests/Cargo.tom
[package]
name = "tests"
version = "0.1.0"
edition = "2021"
publish = false
[dev-dependencies]
crate1 = { path = "../crate1" }
[[test]]
name = "crate1_test"
path = "crate1_test.rs"
[[test]]
name = "other_crate1_test"
path = "other_crate1_test.rs"
this is how one of the tests looks like
// tests/crate1_test.rs
use crate1::random_func;
[test]
fn random_func_test() {
assert!(random_func());
}
And for some reason cargo don't recognize the "crate1" crate and throws me this error each time I import the crate:
error[E0433]: failed to resolve: use of undeclared crate or module `crate1`
--> tests/crate1_test.rs:1:5
|
1 | use crate1::random_func;
| ^^^^^^ use of undeclared crate or module `crate1`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `project-manager` due to previous error

I found the problem, it was that I didn't make the crate1 as a dependency of the main project, this is how my root Cargo.toml looks now:
[package]
name = "project"
version = "0.1.0"
edition = "2021"
[workspace]
members = [
"crate1",
"tests"
]
[dependencies]
crate1 = { path = "crate1" }
reqwest = "0.11.13"
tokio = { version = "1", features = ["full"] }
and now I can build my tests correctly

In src/main.rs you have to add mod crate1; and potentially mod tests;

Related

enabling a feature when using libraries

I'm trying to use DefaultAsyncInterface of tunio which depends on tokio feature (here)
I have added tunio with async-tokio feature in Cargo.toml:
[dependencies]
env_logger = "0.10.0"
etherparse = "0.13.0"
futures = "0.3.25"
netconfig = "0.4.0"
tokio = { version = "1.24.1", features = ["full"] }
tunio = { version = "0.3.0", features = ["async-tokio"] }
and imported it:
use tunio::{DefaultDriver,DefaultAsyncInterface};
but when I try to build I get:
error[E0432]: unresolved import `tunio::DefaultAsyncInterface`
--> src/main.rs:7:27
|
7 | use tunio::{DefaultDriver,DefaultAsyncInterface};
| ^^^^^^^^^^^^^^^^^^^^^
| |
| no `DefaultAsyncInterface` in the root
| help: a similar name exists in the module: `DefaultInterface`
It seems the async-tokio feature is not used
What is the problem?

Unable to access lib.rs file from binary

I am unable to access a library from my binary.
Here is what my cargo.toml looks like
[package]
name = "app"
version = "0.1.0"
edition = "2021"
[dependencies]
<--snip-->
[lib]
path = "src/lib.rs"
[[bin]]
path = "src/main.rs"
name = "realio"
the application root
.
├── Cargo.toml
├── src
│ ├── lib.rs
│ └── main.rs
└── test
└── integration.rs`
and my main.rs
#![crate_name = "realio"]
use env_logger::Env;
use realio::run;
use std::net::TcpListener;
#[tokio::main]
async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let listener = TcpListener::bind("127.0.0.1:8000").expect("failed to bind port");
run(listener)?.await
}
However , I get the following error
error[E0432]: unresolved import `realio`
--> app/src/main.rs:4:5
|
4 | use realio::run;
| ^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `realio`
I would appreciate pointers on this
You're missing the name for the lib.
This would be right:
[dependencies]
[lib]
name = "realio"
path = "src/lib.rs"
[[bin]]
name = "realio"
path = "src/main.rs"
But you don't need to manually declare it there if you stick with the main.rs and lib.rs naming convention. Also keep in mind to change the Package name (line 2 in your Cargo.toml) to "realio", so your code still works.
You can find more infos for that in the Cargo Book: https://doc.rust-lang.org/cargo/guide/project-layout.html

Getting error while using #[js_export] from crate stdweb

I need to read client file in a rust-wasm program and I am trying two solution given on stackoverflow https://stackoverflow.com/a/51231915 and https://stackoverflow.com/a/69305956/4466255. Both these solutions are accepted and given bounties so I am assuming they work.
both of these have used following code
use stdweb::js_export
#[js_export]
But when i try to compile these codes I got following error
error[E0425]: cannot find function `__web_free` in module `stdweb::private`
--> src/lib.rs:14:1
|
14 | #[js_export]
| ^^^^^^^^^^^^ not found in `stdweb::private`
|
= note: this error originates in the attribute macro `js_export` (in Nightly builds, run with -Z macro-backtrace for more info)
What I am missing. How can we make it work?
My current code, which I have taken from stackoverflow https://stackoverflow.com/a/51231915, in lib.rs is
#[macro_use]
extern crate stdweb;
use stdweb::js_export;
use stdweb::web::FileReader;
use stdweb::web::FileReaderResult;
#[js_export]
fn print_result(file_reader: FileReader) -> String {
match file_reader.result() {
Some(value) => match value {
FileReaderResult::String(value) => value,
_ => String::from("not a text"),
}
None => String::from("empty")
}
}
pub fn test_reader() {
stdweb::initialize();
stdweb::event_loop();
}
and the Cargo.toml file is
[package]
name = "read-file2"
version = "0.1.0"
authors = ["kushdesh"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.63"
stdweb = "0.4.20"
console_error_panic_hook = { version = "0.1.6", optional = true }
wee_alloc = { version = "0.4.5", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.13"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

Resolving imports in Rust

I'm having trouble with importing rand crate from crates.io. After adding the line rand="0.8.3" and then running command cargo build for the project, it keeps displaying the same errors:
error[E0432]: unresolved import `rand`
--> main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing crate `rand`?
error[E0433]: failed to resolve: use of undeclared crate or module `rand`
--> main.rs:4:25
|
4 | let secret_number = rand::thread_rng().gen_range(1..=11);
| ^^^^ use of undeclared crate or module `rand`
error: aborting due to 2 previous errors
the cargo.toml file
[package]
name = "roller"
version = "0.1.0"
authors = ["User"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.3"
Basically the simplest reproducible example is this single line of code:
use rand::Rng;
fn main(){
let secret_number = rand::thread_rng().gen_range(1..=11);
print!("{}",secret_number);
}
What's wrong with it?
Just in case:
The **cargo.lock**file:
# This file is automatically #generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core",
]
[[package]]
name = "roller"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
In file Cargo.toml add lines:
[dependencies]
rand="0.3.14"
And rebuild project!
It worked with these versions:
rustc 1.53.0
cargo 1.53.0
Took answer from here https://github.com/rust-lang/rust/issues/34616
got the same error when I run it with vs code(rust-analyzer).
When you press the run button
rustic main.rs this command is called in terminal, it will cause error.
Type this in terminal,
cargo build
cargo run
It works well
If you think that you did everything right and get a strange unable to import error like this then you could try cargo clean.
That will allow you to fully re-build your binary/library after that.

Why building with rustc command cannot see crates?

Hi I'm trying to explore Rust. What I'm trying to do is using glob module, when i build code with $cargo build and $cargo run it's succesfully builds and runs the executable but if i try it with $rustc main.rs it gives me
error[E0432]: unresolved import `glob`
--> src/main.rs:1:5
|
1 | use glob::glob;
| ^^^^ use of undeclared type or module `glob`
Any ideas?
Version : ╰─ rustc --version rustc 1.43.1 (8d69840ab 2020-05-04)
Code is here:
use glob::glob;
fn main() {
for entry in glob("/home/user/*.jpg").unwrap(){
match entry {
Ok(path) => println!("{:?}", path.display()),
Err(e) => println!("{:?}",e),
}
};
}
My toml
[package]
name = "test1"
version = "0.1.0"
authors = ["ieuD <example#mail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
glob = "0.3.0"
rustc on its own doesn't handle your dependencies, it just compiles stuff. When you run rustc on your file, it will start compiling it and encounter the unknown glob crate. Dependencies are handled by cargo via Cargo.toml.
Though you could only use rustc (see the answer here) it's a task that's considerably more difficult, that's why cargo is around.

Resources