How to enable unstable Rust feature str_split_once? - rust

I'm having a hard time figuring out where do I need to write something to enable this feature.
I tried adding #![feature(str_split_once)] to the file where I'm using it but nothing happens. By Googling I found How do you enable a Rust "crate feature"? but after adding
[features]
default = ["str_split_once"]
to Cargo it doesn't build with
Caused by: feature default includes str_split_once which is
neither a dependency nor another feature

I tried adding #![feature(str_split_once)] to the file where I'm using it but nothing happens.
I guess there's not really "nothing happens", but there was a warning similar to this one:
warning: crate-level attribute should be in the root module
--> src/lib.rs:2:5
|
2 | #![feature(str_split_once)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Playground
Just add this line at the beginning of lib.rs and/or main.rs, build with nightly, and it should work:
#![feature(str_split_once)]
fn main() {
// prints Some(("test1", "test2 test3"))
println!("{:?}", "test1 test2 test3".split_once(" "));
}
Playground

Related

could not find yew in stylist

I'm new to rust and yew and trying to use stylist crate with yew but when I try to import styled_components, I am getting following error.
error[E0432]: unresolved import `stylist::yew`
--> src\lib.rs:2:14
|
2 | use stylist::yew::styled_component;
| ^^^ could not find `yew` in `stylist`
For more information about this error, try `rustc --explain E0432`.
error: could not compile `hello-yew` due to previous error
2022-11-22T16:30:53.476368Z ERROR error
error from HTML pipeline
Has anyone seen this issue before?
I tried to delete previous stylist crates and tried to re-install the crate again but faced same issue.
From the docs:
Yew Integration
To enable yew integration. Enable feature yew_integration in Cargo.toml.
You can create a style and use it with yew like this:
use yew::prelude::*;
use stylist::yew::styled_component;
#[styled_component(MyStyledComponent)]
fn my_styled_component() -> Html {
html! {<div class={css!("color: red;")}>{"Hello World!"}</div>}
}

Why are Bevy's Trait bounds not satisfied for the Rapier physics plugin?

I've been trying to run this minimal example to get Rapier's physics working with Bevy:
use bevy::prelude::*;
use bevy_rapier2d::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
.run();
}
and it's failing:
error[E0277]: the trait bound `bevy_rapier2d::plugin::RapierPhysicsPlugin: Plugin` is not satisfied
--> src/main.rs:8:21
|
8 | .add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Plugin` is not implemented for `bevy_rapier2d::plugin::RapierPhysicsPlugin`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `Plugin`:
AnimationPlugin
AssetCountDiagnosticsPlugin<T>
AssetPlugin
AudioPlugin
BloomPlugin
CameraPlugin
CameraProjectionPlugin<T>
ColorMaterialPlugin
and 44 others
note: required by a bound in `bevy::prelude::App::add_plugin`
--> /home/techperson/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_app-0.9.0/src/app.rs:837:12
|
837 | T: Plugin,
| ^^^^^^ required by this bound in `bevy::prelude::App::add_plugin`
For more information about this error, try `rustc --explain E0277`.
Expected behavior is what is described in the Rapier documentation.
Some info:
$ cargo version
cargo 1.66.0-beta.1 (7e484fc1a 2022-10-27)
$ rustup show
Default host: x86_64-unknown-linux-gnu
rustup home: /home/techperson/.rustup
installed toolchains
--------------------
stable-x86_64-unknown-linux-gnu
beta-x86_64-unknown-linux-gnu (default)
nightly-x86_64-unknown-linux-gnu
active toolchain
----------------
beta-x86_64-unknown-linux-gnu (default)
rustc 1.66.0-beta.1 (e080cc5a6 2022-11-01)
Relevant part of Cargo.toml:
[dependencies]
bevy = "0.9.0"
bevy_rapier2d = "0.18.0"
I tried manually implementing the Plugin trait, but can't because its from a different crate:
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> src/main.rs:4:1
|
4 | impl Plugin for RapierPhysicsPlugin {}
| ^^^^^^^^^^^^^^^^-------------------
| | |
| | `bevy_rapier2d::plugin::RapierPhysicsPlugin` is not defined in the current crate
| impl doesn't use only types from inside the current crate
|
= note: define and implement a trait or new type instead
For more information about this error, try `rustc --explain E0117`.
I've also tried the stable, beta, and nightly toolchains. beta and nightly fail with the aforementioned error, and stable fails because if-let statements aren't stable.
Bevy 0.9 is an extremely recent release, 3 days ago at the time of writing. It changed quite a lot about bevy internals, especially the trait system, as can be expected of an unstable project at this stage.
Most of the ecosystem will be upgrading over the course of the next few weeks. Revert back to bevy 0.8 and you should be fine for now.
I have not really tested it, only recompiled code adding the plugin and directly causing the above error. But at very least this (temporary solution) prevents that error from occurring (need to look at it bit more, but I have ran out of time for today): https://github.com/mariasiuvlad/bevy-game/pull/5/files#diff-2e9d962a08321605940b5a657135052fbcef87b5e360662bb527c96d9a615542R12
TL;DR:
bevy_rapier2d = { git = "https://github.com/devil-ira/bevy_rapier", branch = "bevy-0.9" }
EDIT: Rapier seems to have caught up with 0.9 now, so this is now inconsequential.

cargo rust build script - print output of command

I am new to rust and cargo, and I am trying to do something very simple!
I have something like this (in build.rs):
use std::process::Command;
fn main() {
Command::new("echo 123");
}
And I want to see the output of the command echo 123. I want 123 to get printed to the build output (this is mostly to debug what I am doing) and wont be part of the final project.
I have tried cargo build --verbose - this does not work.
I can't extrapolate an answer from there posts (and some others like it):
https://github.com/rust-lang/cargo/issues/985
https://github.com/rust-lang/cargo/issues/1106
I feel this must be simple to do - but I have been stuck for hours looking on the web and not finding the answer.
Just building a Command with Command::new does not execute it yet. It just starts a builder pattern. To actually execute it, you have to use the methods spawn, output or status. Example:
Command::new("echo")
.arg("123")
.spawn()
.expect("failed to spawn process");
It's very unfortunate that this doesn't produce a warning. Someone recently tried to add the #[must_use] attribute to Command, which would make your code procude a warning. The PR closed for now but it seems like it will be added eventually.
We can use a macro and it worked form me, but there is a warning, since it uses cargo to display. but that is fine for me.
I found below code from git hub discussion:
Cargo doesn’t display output from a command in build.rs #985
macro_rules! p {
($($tokens: tt)*) => {
println!("cargo:warning={}", format!($($tokens)*))
}
}
fn main() {
p!("BUILD.rs -> Starting ...");
}

Why does `cargo build` not show all errors in my code?

This code doesn't compile:
extern crate iron;
#[marco_use] //misspelled here
extern crate mime;
use iron::prelude::*;
use iron::status;
fn main() {
let mut response = Response::new();
response.set_mut(mime!(Text/Html; Charset=Utf8));
}
it shows:
error: cannot find macro `mime!` in this scope
--> src/main.rs:10:22
|
10 | response.set_mut(mime!(Text/Html; Charset=Utf8));
| ^^^^
If I add extern crate hyper; use hyper::mime::*;, then it shows:
error: The attribute `marco_use` is currently unknown to the compiler and
may have meaning added to it in the future (see issue #29642)
--> src\main.rs:2:1
|
2 | #[marco_use] extern crate mime;
| ^^^^^^^^^^^^
If I could've seen this earlier, it would've helped me to fix the mistake...
I guess Cargo only shows one error? I could not find anything about this behaviour online. How can I see all errors?
The compilation process is divided into several stages and if during one of them an error breaks the build, the following stages are not processed further. This is not specific to Cargo, but rustc as well (example: When are numeric literals assigned to default types?).
I haven't seen it officially documented, but the high-level process has been described by japaric:

How do I build code in a documentation test but not run it?

I have code in my documentation that can only be run if the user has some software on their machine. To emulate this, I add panic! to the sample code:
//!```rust
//!fn main() {
//! panic!("Not run me");
//!}
//!```
#[cfg(test)]
mod tests {
#[test]
fn it_works() {}
}
I want to check that the code in the comments can be compiled, but I do not want it to be run during cargo test. Right now, I get:
running 1 test
test src/lib.rs - (line 1) ... FAILED
failures:
---- src/lib.rs - (line 1) stdout ----
thread 'rustc' panicked at 'test executable failed:
thread 'main' panicked at 'Not run me', <anon>:2
note: Run with `RUST_BACKTRACE=1` for a backtrace.
I read about doctest = false, but that disables not only the running of code in comments, but also syntax checking the code in comments.
How can I only disable running of code in comments, but still enable compilation of code in comments during cargo test?
There are several annotations you can use to change how the Rust code is processed. See the test documentation.
In your case it sounds like no_run is the one you'd want
//!```rust,no_run
//!fn main() {
//! panic!("Not run me");
//!}
//!```
Alternatively you could use should_panic so Rust will run the code, but expect the panic. If it's code that won't actually compile, you can use ignore.

Resources