Why does Rust Analyser disagree with the compiler? [closed] - rust

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm using the latest stable version: rustc 1.46.0 (04488afe3 2020-08-24).
Rust Analyser needs the import:
use chrono::offset::TimeZone;
in order to accept the expression:
Utc.ymd(1970, 1, 1).and_hms_milli(0, 0, 0, 200)
(It highlights the .ymd as {unknown}.)
If I add the import, the compiler issues a warning:
Compiling foo v0.1.0 (/home/fadedbee/foo)
warning: unused import: `chrono::offset::TimeZone`
--> src/bar.rs:2:5
|
2 | use chrono::offset::TimeZone;
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
How can I make Rust Analyser (in vscode), and the compiler, both happy?
Update:
As rodrigo correctly deduced, the only uses of .ymd are within a #[cfg(test)] section.

Related

use of undeclared crate or module `proc_macro2` [closed]

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.

Rust compiler error - "maybe missing crate ..." [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a project with multiple files which all depend on each other which worked fine. I added another file and added the use crate::asteroid::Asteroid syntax to another file.
When I compile it says
unresolved import "crate::asteroid"
This worked with all the other files. What is wrong?
Project layout:
/src/
asteroid.rs
command.rs
direction.rs
game.rs
main.rs
point.rs
ship.rs
The use keyword will import only the path specified, so when you use crate::asteroid::Asteroid only the Asteroid object will be imported, but not crate::asteroid. In order to import both, you can use:
use crate::asteroid::{ self, Asteroid };
Here self is referring to crate::asteroid. You will then be able to access both asteroid and Asteroid

use web_sys::HtmlButtonElement fails [duplicate]

This question already has an answer here:
How do you enable a Rust "crate feature"?
(1 answer)
Closed 3 years ago.
Managed to build and run some of the examples in https://github.com/rustwasm/wasm-bindgen/tree/master/examples
Then started with little prototype program for Dom manipulation, things works, until stuck on this.
Use
https://docs.rs/web-sys/0.3.35/web_sys/struct.HtmlButtonElement.html
lists pub struct HtmlButtonElement, similar to
https://docs.rs/web-sys/0.3.35/web_sys/struct.Element.html and
https://docs.rs/web-sys/0.3.35/web_sys/struct.HtmlElement.html
Having:
use web_sys::Element;
use web_sys::HtmlElement;
use web_sys::HtmlButtonElement;
Gives compile error:
error[E0432]: unresolved import `web_sys::HtmlButtonElement`
--> src/lib.rs:8:5
|
8 | use web_sys::HtmlButtonElement;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ no `HtmlButtonElement` in the root
While Element and HtmlElement are found in web_sys
What is the difference/missing for HtmlButtonElement? Does the no HtmlButtonElement in the root message give a clue?
Following the comment by #chpio. When adding HtmlButtonElement to features in Cargo.toml HtmlButtonElement is found when building.

Rust integration test can't `use` library [closed]

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...

How to include an arbitrary markdown file as a documentation attribute? [duplicate]

This question already has answers here:
How is it possible to keep Rust module documentation in separate Markdown files?
(3 answers)
Closed 3 years ago.
If the readme Cargo.toml key is set, doc.rs renders the README on the crate's index page. Is there a way to emulate this when running cargo doc locally?
If I add:
#![doc = r###"contents
of
README.md
here
"###]
as a literal, I get the behaviour I'm looking for, but inlining a copy of my whole README.md is pretty inconvenient for making updates.
I tried:
#![doc = include!("README.md")]
but this gives an error:
error: unexpected token: `include`
--> src/lib.rs:3:10
|
3 | #![doc = include!("README.md")]
| ^^^^^^^
There is an unstable feature, external-doc, which enables this:
Example usage (nightly-only):
#![feature(external_doc)]
#![doc(include = "../README.md")]

Resources