I am trying to use BitSet data structure, but it gives me a compile error saying it was not able to find the BitSet. Has std::collections::BitSet been released in the stable version?
use std::collections::BitSet;
fn main() {
println!("Hello, world!");
}
Produces the error:
error[E0432]: unresolved import `std::collections::BitSet`
--> src/main.rs:1:5
|
1 | use std::collections::BitSet;
| ^^^^^^^^^^^^^^^^^^^^^^^^ no `BitSet` in `collections`
It seems that BitSet existed in Rust 1.3.0, which is very old, but was already deprecated at that time and finally removed by this commit.
Instead, you can use bit-set crate, as suggested by the deprecation message above. There's also documentation.
extern crate bit_set;
use bit_set::BitSet;
fn main() {
let mut s = BitSet::new();
s.insert(32);
s.insert(37);
s.insert(3);
println!("s = {:?}", s);
}
You'll have to add a dependency to the bit-set crate in some way. It's easy if you're using Cargo:
[package]
name = "foo"
version = "0.1.0"
authors = ["Foo Bar <foo#example.com>"]
[dependencies]
bit-set = "0.4.0" # Add this line
If you're using the official Rust Playground, you can automatically use bit-set, because it is one of the top 100 downloaded crates or a dependency of one of them.
Related
I am learning yewdux and have implemented the tutorial code for global state:
use yew::prelude::*;
use yewdux::prelude::*;
#[derive(Default, Clone, PartialEq, Eq, Store)]
struct State {
count: u32,
}
#[function_component]
fn App() -> Html {
let (state, dispatch) = use_store::<State>();
let onclick = dispatch.reduce_mut_callback(|state| state.count += 1);
html! {
<>
<p>{ state.count }</p>
<button {onclick}>{"+1"}</button>
</>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
However I am getting a compiler error for the line:
let (state, dispatch) = use_store::<State>();
The compiler error reads:
error[E0277]: the trait bound `impl yew::functional::hooks::Hook<Output = (Rc<State>, Dispatch<State>)>: Hook` is not satisfied
--> src/main.rs:11:29
|
11 | let (state, dispatch) = use_store::<State>();
| ---------^^^^^^^^^^^
| |
| the trait `Hook` is not implemented for `impl yew::functional::hooks::Hook<Output = (Rc<State>, Dispatch<State>)>`
| required by a bound introduced by this call
|
= help: the trait `Hook` is implemented for `BoxedHook<'_, T>`
My Cargo.toml file is:
[package]
name = "yewdux_tutorial"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] }
stdweb = "0.4.20"
yewdux = "0.9.0"
Please could someone help point me in the right direction for solving this compiler error.
I have searched online for this this answer and found this question Failed to follow yew tutorial on Mac m1 - use of undeclared type `Vec` (Also on a mac m1) and followed the answer to no success.
I also attempted to manually implement a default Store on the State struct but that also did not fix it.
Try to add yew crate this way:
[dependencies]
yew = { version = "0.20", features = ["csr"] }
yewdux = "0.9"
When we did not specify yew by its source, we actually refer to the one in the official crate registry. But if we specify yew by its git repository, we refer to the crate at the default branch if no branch specified. The two crates are not the same in the eye of the Rust compiler.
The crate yewdux in the official crate registry probably also relies on the yew that is in the official crate registry as well, so we need to refer to yew in the official crate registry.
The file Cargo.lock shows more details about crate dependencies. If source = "registry+https://github.com/rust-lang/crates.io-index", then the crate is from the official crate registry. If source = "git+https://github.com/yewstack/yew.git#c40bd0b456f9c80146c99d56bc0b441c88c3fefe", then the crate is from the repo specified and the code version is at the commit c40bd0b456f9c80146c99d56bc0b441c88c3fefe.
Reference: https://intendednull.github.io/yewdux/setup.html#stable-release
I'm starting on a command-line tool in Rust, and hitting a wall right from the get-go. I can parse command-line arguments using StructOpt if the Opt struct is defined in main.rs, but since I want to be able to pass the Opt struct into the library code, I'm defining it in its own file so that other parts of the library know what it's all about.
Here's the version of the code that's dumping the fewest errors, although I concede that it's largely cobbled together by trying things suggested by the compiler and some random SO suggestions, so it may be completely wrong.
The build error I'm getting is:
$ cargo run
Compiling basic v0.1.0 (/home/mpalmer/src/action-validator/blobble)
error[E0433]: failed to resolve: maybe a missing crate `structopt`?
--> src/opt.rs:8:5
|
8 | /// Activate debug mode
| ^^^^^^^^^^^^^^^^^^^^^^^ not found in `structopt::clap`
|
help: consider importing this struct
|
3 | use opt::structopt::clap::Arg;
|
For more information about this error, try `rustc --explain E0433`.
error: could not compile `basic` due to previous error
$ cargo --version
cargo 1.56.0 (4ed5d137b 2021-10-04)
$ rustc --version
rustc 1.56.0 (09c42c458 2021-10-18)
(Yes, I have tried adding use opt::structopt::clap::Arg;, just in case, but the error doesn't go away and I get a warning about an unused import -- while still being told to try adding the same use that is unused, which is amusing)
Cargo.toml
[package]
name = "basic"
version = "0.1.0"
authors = ["User"]
[dependencies]
structopt = "0.3"
src/main.rs
extern crate basic;
extern crate structopt;
use basic::Opt;
use structopt::StructOpt;
fn main() {
let opt = Opt::from_args();
println!("{:#?}", opt)
}
src/lib.rs
mod opt;
pub use crate::opt::Opt;
src/opt.ts
extern crate structopt;
use self::structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
pub struct Opt {
/// Activate debug mode
#[structopt(short,long)]
debug: bool,
}
Suggestions gratefully appreciated.
A working version is
Cargo.toml
[package]
name = "basic"
version = "0.1.0"
authors = ["User"]
edition = "2018"
[dependencies]
structopt = "0.3"
lib.rs
#[macro_use] extern crate structopt;
use structopt::StructOpt;
pub mod opt;
opt.rs
#[derive(StructOpt, Debug)]
#[structopt(name = "basic")]
pub struct Opt {
/// Activate debug mode
#[structopt(short,long)]
debug: bool,
}
main.rs
#[macro_use] extern crate structopt;
use structopt::StructOpt;
fn main() {
let opt = basic::opt::Opt::from_args();
println!("{:#?}", opt);
}
You need to declare use structopt::StructOpt because from_args trait must be in the scope.
I am trying to use tokios background event loop feature, but I am unable to use any tokio reactor features and I have no idea why. I created a new project with cargo new tokio-test --bin and copy-pasted the code snippet and in the link for establishing a basic TCP connection.
Cargo.toml
[package]
name = "tokio-test"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1.11.0", features = ["full"] }
main.rs
use tokio::prelude::*;
use tokio::net::TcpStream;
fn main() {
let addr = "93.184.216.34:9243".parse().unwrap();
let connect_future = TcpStream::connect(&addr);
let task = connect_future
.and_then(|socket| {
println!("successfully connected");
Ok(())
})
.map_err(|e| println!("failed to connect; err={:?}", e));
tokio::run(task);
}
When running cargo build I get:
error[E0432]: unresolved import `tokio::prelude`
--> src/main.rs:1:12
|
1 | use tokio::prelude::*;
| ^^^^^^^ could not find `prelude` in `tokio`
error[E0425]: cannot find function `run` in crate `tokio`
--> src/main.rs:16:12
|
16 | tokio::run(task);
| ^^^ not found in `tokio`
error[E0599]: no method named `and_then` found for opaque type `impl Future` in the current scope
--> src/main.rs:10:10
|
10 | .and_then(|socket| {
| ^^^^^^^^ method not found in `impl Future`
|
help: consider `await`ing on the `Future` and calling the method on its `Output`
|
10 | .await.and_then(|socket| {
| ^^^^^^
Clearly I am missing some directive from tokio but I cannot figure how to include it. My rustc version is rustc 1.55.0 (c8dfcfe04 2021-09-06).
You're depending on tokio v1.11.0 (the current release), but your docs are for v0.1.22. The interface has changed quite drastically, that's why you are not finding all those types, functions and modules. Current documentation is here.
If you found the documentation via Google: The problem of Google returning old docs.rs results is well known. There is an open issue that will solve this problem.
Also, docs.rs displays a warning on the top left of the page to go to the latest version if you are not on that version anyway. You should get in the habit of clicking it unless you know you're using an older version. (Thanks to kmdreko for pointing this out in the comments)
I'm trying to learn some rust by my usual method of flailing around and running examples.
This page on the rust_contrib api docs makes me think I can serve static files https://api.rocket.rs/master/rocket_contrib/serve/struct.StaticFiles.html like this...
use rocket_contrib::serve::StaticFiles;
use rocket_contrib::serve::crate_relative;
...
fn main() {
rocket::ignite()
.mount("/content", StaticFiles::from(crate_relative!("content")))
.mount("/", routes![index])
.mount("/api", routes![hello, new_book])
.register(catchers![not_found])
.attach(Template::fairing())
.launch();
}
Instead the compiler barks at me...
error[E0432]: unresolved import `rocket_contrib::serve::crate_relative`
--> src/main.rs:9:5
|
9 | use rocket_contrib::serve::crate_relative;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `crate_relative` in `serve`
Cargo.toml looks like this...
[package]
name = "rocket-web"
version = "0.1.0"
authors = ["Nunya B. Znas <dontworry#bout.it>"]
edition = "2018"
[dependencies]
rocket = "0.4.5"
serde = {version = "1.0", features = ["derive"]}
[dependencies.rocket_contrib]
version = "0.4.5"
features = ["handlebars_templates", "tera_templates"]
What have I done to offend the rust gods?
Thanks
The docs you are looking at are for Rocket's master branch, which currently represents the yet-to-be-released 0.5 version. Rocket version 0.4.5 does not have crate_relative.
For a workaround, you can see how the 0.4 docs say to do it:
use rocket_contrib::serve::StaticFiles;
fn main() {
rocket::ignite()
.mount("/", StaticFiles::from(concat!(env!("CARGO_MANIFEST_DIR"), "/static")))
.launch();
}
I have a hashbrown::HashSet, and I am trying to use Rayon's par_iter with it, but I cannot figure out the right syntax.
Cargo.toml
[package]
name = "basic"
version = "0.1.0"
authors = ["Me <me#example.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hashbrown = "0.9.1"
rayon = "1.5.0"
src/main.rs
use hashbrown::*;
use rayon::prelude::*;
// use hashbrown::hash_set::rayon::*;
fn main() {
println!("Hello, world!");
let hs = HashSet::new();
for n in 1..100 {
hs.insert(n);
}
hs.par_iter().for_each(|n| println!("n={}", n));
}
This fails to compile.
error[E0599]: no method named `par_iter` found for struct `hashbrown::HashSet<{integer}>` in the current scope
--> src/main.rs:13:8
|
13 | hs.par_iter().for_each(|n| println!("n={}", n));
| ^^^^^^^^ method not found in `hashbrown::HashSet<{integer}>`
|
::: /Users/me/.cargo/registry/src/github.com-1ecc6299db9ec823/hashbrown-0.9.1/src/set.rs:114:1
|
114 | pub struct HashSet<T, S = DefaultHashBuilder> {
| --------------------------------------------- doesn't satisfy `_: rayon::iter::IntoParallelRefIterator`
|
= note: the method `par_iter` exists but the following trait bounds were not satisfied:
`&hashbrown::HashSet<{integer}>: IntoParallelIterator`
which is required by `hashbrown::HashSet<{integer}>: rayon::iter::IntoParallelRefIterator`
warning: unused import: `rayon::prelude`
--> src/main.rs:2:5
|
2 | use rayon::prelude::*;
| ^^^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
The hashset-rayon docs suggest that hashbrown::hash_set::rayon has a Rayon-related data type, and I know that sometimes I need to use traits to make functionality available. However, if I uncomment the import, it cannot even find the module:
error[E0432]: unresolved import `hashbrown::hash_set::rayon`
--> src/main.rs:3:26
|
3 | use hashbrown::hash_set::rayon::*;
| ^^^^^ could not find `rayon` in `hash_set`
I am running Rust 1.49.0 on a Mac:
$ rustup show
Default host: x86_64-apple-darwin
rustup home: /Users/tda0106/.rustup
installed toolchains
--------------------
stable-x86_64-apple-darwin (default)
nightly-x86_64-apple-darwin
active toolchain
----------------
stable-x86_64-apple-darwin (default)
rustc 1.49.0 (e1884a8e3 2020-12-29)
What is the problem?
Edit
As #E_net4 the curator pointed out in the comments, the rayon support is in a feature. Changing the dependencies to
[dependencies]
hashbrown = { version = "0.9.1", features = ["rayon"] }
rayon = "1.5.0"
makes this work, without needing the extra use statement.
It's not clear to me where the documentation indicates this.
If you look inside of the hashbrown crate, you will see that rayon support is only present if the rayon feature is enabled. You can enable the feature in your Cargo.toml:
[dependencies]
hashbrown = { version = "0.9.1", features = ["rayon"] }