use web_sys::HtmlButtonElement fails [duplicate] - rust

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.

Related

how to change .multiple in upgrade to rust clap v4

I am trying to upgrade rust code from clap v3.22.2 to v4.0.8 and faced the problem how to change Arg::multiple. This is piece of code:
Arg::new("relfs")
required(true)
multiple(true)
So I get an error
error[E0599]: no method named `multiple` found for struct `Arg` in the current scope
--> src/bin/reo.rs:178:26
|
178 | .multiple(true)
| ^^^^^^^^ method not found in `Arg`
I already tried to Arg::action as written in https://github.com/clap-rs/clap/issues/3772 but couldn't.
How can replace deprecated multiple?
Eventually changelog helped me. There is the information what has been changed and how to update.
In my case it was deprecated multiple.

Why does Rust Analyser disagree with the compiler? [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 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.

Rust: Cannot access functions in subdirectories [duplicate]

This question already has an answer here:
Can't understand Rust module system
(1 answer)
Closed 2 years ago.
I am learning rust and am going through the book and have run into a question. Is it possible to access files within a folder? I am currently having issues with this.
Here is a project which demonstrates the issue: https://github.com/joemspalding/rust-stack-overflow-file-example/tree/master/src
In this project, I can successfully import functions from a different file, however, trying to access a file from a different folder is where I lose understanding syntactically. I feel I've tried nearly every combination of
mod foo;
mod foo::bar;
use foo;
use foo::bar;
Currently, running cargo run produces the following compile time error
Compiling my-project v0.1.0 (J:\Projects\rust\the_book\ch7\my-project)
error[E0432]: unresolved import `foo`
--> src\main.rs:1:5
|
1 | use foo::bar;
| ^^^ use of undeclared type or module `foo`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0432`.
error: could not compile `my-project`.
To learn more, run the command again with --verbose.
I've been reading into this for hours and all of the resources I can find have great examples of reading code from a file on the same level, but not on reading code through folders.
Another common line I've seen is with the use of folder/mod.rs This seems unstylistic of rust after an update in 2018 and I'd like to avoid it if possible.
You must use mod.rs. It is the preferred option in rust 2018 and above. Module definitions aren't file paths and shouldn't be treated as such. Think of them more as C++ namespaces.
You could also choose to rename your mod.rs to match the name of the folder it is in (Ex: example_a/mod.rs -> example_a/example_a.rs). However I almost never this done and I find its much easier to navigate code with mod.rs.
If your mod.rs doesn't have any useful code, maybe you don't need a submodule.
A mod.rs should have code which relates to its sub modules. If it is only a couple lines long, you may want to skip adding the folder entirely since it doesn't sound like it has any purpose in your specific use case. This isn't like the empty __init__.py files of python 2.

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")]

Typing hints PyCharm for a class used within itself [duplicate]

This question already has answers here:
Type hints: solve circular dependency [duplicate]
(2 answers)
Closed 3 years ago.
How do I properly type a class within itself? In PyCharm I am currently getting this error:
This is an unresolved reference error. This normally makes sense because I wouldn't expect PyCharm to support types perfectly. However, when I use it in other classes besides the Item class itself there is no error:
Thus I believe that the error only appears when the type hint is supplied within its own class. So I don't know what exactly to do to prevent this error or if I am using types wrong in general and a type shouldn't be used within itself.
Basically the behavior I am trying to emulate is that you have a crafting recipe for an item and can create new instances of that item with the recipe function.
When using a class as a type inside that class, or anywhere where that type is not fully defined yet, you need to enclose the type in single or double quotes in your annotations:
class Item:
...
def craft(self, substrates: List['Item'], amount: int) -> List['Item']:
...
Sources:
https://www.python.org/dev/peps/pep-0484/#forward-references
https://blog.jetbrains.com/pycharm/2015/11/python-3-5-type-hinting-in-pycharm-5/
(String-Based Hints)
Edit: PEP 563: https://www.python.org/dev/peps/pep-0563/ improves upon this.

Resources