Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Working to include integration tests to my project but I'm unable to import the library. I thought the new rules would allow me to just write a use statement but it isn't going very well :)
The code below shows the related components. Isn't this supposed to be valid?
Cargo.toml
[package]
name = "myswankynewpackage"
version = "0.1.0"
authors = ["Me Myself <me.myself#gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
tests/tests.rs
use myswankynewpackage;
// Also tried extern crate
// extern crate myswankynewpackage;
#[cfg(test)]
mod integration {
use super::*;
mod module{
#[test]
fn module_test() {
}
}
}
I get an error saying it can't find the crate
error[E0432]: unresolved import `myswankynewpackage`
--> tests/tests.rs:1:5
|
1 | use myswankynewpackage;
| ^^^^^^^^^^^^^^^^^^ no `myswankynewpackage` external crate
So, I noticed that OP says the actual Cargo.toml matches the given one "except [OP is] ... using a couple of libraries."
I think what's causing problems is the libraries - if you have libraries (with a different name), you need to use the name of the library in the use statement. ie:
Cargo.toml
[package]
name = "myswankynewpackage"
version = "0.1.0"
authors = ["Me Myself <me.myself#gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
tests/tests.rs
[lib]
name = "myswankynewlib"
path = "src/lib.rs"
tests/tests.rs
// WRONG!
// use myswankynewpackage;
// RIGHT!
use myswankynewlib;
...
I had a similar error message, and originally found this post when searching before I realized the issue. So even if this WASN'T the original poster's problem, perhaps this answer will help others...
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I just tried defining a derive macro using the proc_macro2 module, using the copy-pasted code from the proc_macro2 documentation:
extern crate proc_macro;
#[proc_macro_derive(MyTrait)]
pub fn my_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = proc_macro2::TokenStream::from(input);
let output: proc_macro2::TokenStream = {
/* transform input */
};
proc_macro::TokenStream::from(output)
}
However, when trying to compile the preceding example, I obtain the lovely error
13 | let output: proc_macro2::TokenStream = {
| ^^^^^^^^^^^ use of undeclared crate or module `proc_macro2`
|
The Cargo.toml file contains the line proc-macro2 = "1.0" and this indeed made a proc-macro2-1.0.49 directory appear under ~/.cargo/registry/src.
I also tried appending extern crate proc_macro2, to no avail (“error[E0463]: can't find crate for proc_macro2”). (Strangely, doing a strace on the compiler shows that it does read the crate files).
rustc version is 1.66.0.
What did I miss?
Edit: found it. This proc-macro crate was a subcrate of a main (binary) one and of course I was adding proc-macro2 to the wrong Cargo.toml file.
Background
I'm currently in the process of writing a crate that's bindings for a C library and I need the user to specify where the built library is. Previously, I've seen this handles by the llvm-sys crate using environment variables. However, having used this a lot it can become quite cumbersome to have to write this every time I want to run a project.
similar questions
I found this post that asked a similar thing, but seemed to only be able to use specified tags, so not able to specify a path.
What I want to do
I would like to know if there's any way I can have a specific custom value in the Cargo.toml file. The idea being the crate I'm writing would require a specific value (I'll call it example here) that can take any string value. so when this crate were to be used in another project, that projects Cargo.toml file would be similar to this::
[package]
name = "some-other-project"
version = "0.1.0"
edition = "2021"
[dependencies.my_crate]
version = "0.1.0"
example = "something goes here"
This value would then be accessible by the crate I'm writing (the dependency in regards to the above manifest file) and usable in some way.
You can set environment variables using a Cargo configration file (.cargo/config.toml) then you can retrieve them in downstream crates by using e.g. env!()/option_env!()/build scripts.
I am very new to rust and wanted to work through some examples to get a better understanding.
I learned that cargo executes examples similar to a normal app and if there are additional dependencies which are not covered by the crate the examples are written for, there is a region in the TOML where these dependencies may be defined.
druid gives a lot of nice examples which do run when I start them with e.g.
cargo run --example text
Then I get a little window which shows a text and the display can be modified with some radio buttons.
But when I start a new project on my own with cargo, put druid into dependencies and copy the source from the example into main.rs this does not compile.
So what do have to change to get the example running as main?
Michael
The code can be found under
text.rs
My toml looks like this:
[package] name = "test-text"
version = "0.1.0"
authors = ["michael.heisler"]
edition = "2018"
[dependencies]
druid="0.6"
and the first error from a full bunch is:
error[E0432]: unresolved import druid::piet::TextStorage-->src\main.rs:17:42
17 use druid::piet::{PietTextLayoutBuilder, TextStorage as PietTextStorage};
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no TextStorage in piet
Still I would like to know how I find the differences, but with
druid = { git = "https://github.com/linebender/druid.git" }
in the dependencies instead of
druid = "0.6"
the application is working
As discussed in Is it documented that Cargo can download and bundle multiple versions of the same crate?, it's possible for Cargo to pull in multiple versions of the same crate for a single program. How do I access both of these versions concurrently?
As of Rust 1.31, you can rename dependencies in Cargo.toml:
[dependencies]
futures_01 = { package = "futures", version = "0.1.0" }
futures_03 = { package = "futures", version = "0.3.0" }
You can choose whatever name you want for the key. The package attribute needs to be the official name of the crate.
Within your code, you can access version 0.1.x using the crate name futures_01, and version 0.3.x via futures_03.
See also:
How to idiomatically alias a crate in Rust 2018?
Why is a trait not implemented for a type that clearly has it implemented?
I have a crate foo_sys. In Rust 2015 I used extern crate foo_sys as foo for convenience, but in Rust 2018 extern crate isn't needed anymore and I don't want to use it only for aliasing. When dropping extern crate, I get
error[E0463]: can't find crate for foo
This can be achieved with the rename-dependency Cargo feature, available in Rust 1.31. With this feature, it's possible to provide a package attribute to the dependencies:
The rename-dependency feature allows you to import a dependency with a different name from the source. This can be useful in a few scenarios:
Depending on crates with the same name from different registries.
Depending on multiple versions of a crate.
Avoid needing extern crate foo as bar in Rust source.
Instead of writing
[dependencies]
foo_sys = "0.2"
the package key can be added to the dependency in Cargo.toml:
[dependencies]
foo = { package = "foo_sys", version = "0.2" }
WARNING: Cargo prior to Rust 1.26.0 may download the wrong dependency when using this feature!
The idiomatic solution is to rename the crate in Cargo.toml. See the answer by Tim Diekmann for more information about that.
But if you don't want to use Cargo.toml renaming for some reason, you can still use the old syntax. It's soft-deprecated, but not removed. So this still works:
extern crate foo_sys as foo;
(Playground example)