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();
}
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 followed the quickstart guide. Now I'm trying to return some super simple JSON and the documentation is wrong and there's no way to submit a ticket without getting on IRC.
Error
error[E0432]: unresolved import `rocket::serde::json`
--> src/main.rs:2:20
|
2 | use rocket::serde::json::Json;
| ^^^^ could not find `json` in `serde`
For more information about this error, try `rustc --explain E0432`.
error: could not compile `my-api` due to previous error
Files
Cargo.toml
[package]
name = "my-api"
version = "0.1.0"
edition = "2021"
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.5.0-rc.1"
serde = "1.0.130"
main.rs
#[macro_use] extern crate rocket;
use rocket::serde::{Serialize, json::Json};
#[derive(Serialize)]
struct Location {
lat: String,
lng: String,
}
#[get("/?<lat>&<lng>")]
fn location(lat: &str, lng: &str) -> Json<Location> {
Json(Location {
lat: 111.1111.to_string(),
lng: 222.2222.to_string(),
})
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![location])
}
If you go here you'll see this is almost a direct copy/paste from the documentation. I don't know enough of Rust to troubleshoot dependency errors.
The json feature for rocket needs to be explicitly turned on in your Cargo.toml.
[package]
name = "my-api"
version = "0.1.0"
edition = "2018" // cut back for testing with nixpkgs-provided rust
publish = false
[dependencies]
serde = "1.0.130"
[dependencies.rocket]
version = "0.5.0-rc.1"
features = ["json"]
This is documented in a comment in the Rocket source which generates the document here.
On top of Charles' answer, I'd change the serde import to:
serde = { version = "1.0", features = ["derive"] }
As documented here
I'd like to return an App instance from a module, but I just don't get anywhere.
I am propably doing everything wrong, that can be done wrong.
Here's my effort so far:
app.rs (called by main.rs)
extern crate actix_web;
extern crate actix_http; // => can't be found
extern crate actix_service; // => can't be found
use actix_web::App;
use actix_http::body::MessageBody;
use actix_service::ServiceFactory;
pub fn create() -> Result<App<MessageBody, ServiceFactory>, Error> {
let app = App::new();
// adding services
Ok(App)
}
There's propably a lot wrong with this code, but at the moment my main problem is that I can't import actix_http and actix_service which both are needed to return the proper types of the App result.
Addendum:
Cargo.toml
[package]
name = "backend"
version = "0.1.0"
authors = ["My Name <my#emai.l>"]
edition = "2018"
[dependencies]
actix-web="3"
diesel= { version = "1.4.5", features = ["mysql"] }
dotenv= { version = "0.15.0" }
[[bin]]
name = "main"
path = "src/main.rs"
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.
I'm following the instructions from here. In the example folder, I use the first example. For some reason console says it can't find the external crate "gl". Here's what my Cargo.toml looks like.
[package]
name = "hello_world"
version = "0.0.1"
authors = [ "bob <bobbuilder#gmail.com>" ]
[dependencies.gl]
git = "https://github.com/bjz/gl-rs"
[dependencies.glfw]
git = "https://github.com/bjz/glfw-rs.git"
[build-dependencies]
gl_generator = "*"
[dependencies]
gl_common = "*"
[[bin]]
name = "hello_world"
My build.rs
extern crate gl_generator; // <-- this is your build dependency
extern crate khronos_api; // included by gl_generator
use std::os;
use std::io::File;
fn main() {
let dest = Path::new(os::getenv("OUT_DIR").unwrap());
let mut file = File::create(&dest.join("gl_bindings.rs")).unwrap();
// This generates bindsings for OpenGL ES v3.1
gl_generator::generate_bindings(gl_generator::GlobalGenerator,
gl_generator::registry::Ns::Gles2,
khronos_api::GL_XML,
vec![],
"3.1", "core", &mut file).unwrap();
}
The documentation you linked to says:
Under the [package] section, add build = "build.rs"
But I don't see that in your Cargo.toml.