Why can my `no_std` program compile even if it depends on `std` somehow - rust

I have a src/lib.rs:
#![no_std]
#[macro_use]
extern crate derive_builder;
extern crate alloc;
#[derive(Builder)]
#[builder(no_std)]
struct Foo {
bar: i32,
}
And Cargo.toml:
[package]
name = "foo"
version = "0.1.0"
authors = ["Foo Bar <foobar#example.com>"]
edition = "2018"
[dependencies]
derive_builder = { version = "0.10.1", default-features = false }
This program compiles with the following command:
cargo build
However, when I specify a target that doesn't have the std crate, it will not compile.
> cargo build --target=x86_64-unknown-uefi -Z build-std=core,alloc,compiler_builtins
Updating crates.io index
Compiling strsim v0.10.0
Compiling fnv v1.0.7
Compiling ident_case v1.0.1
Compiling proc-macro2 v1.0.26
error[E0463]: can't find crate for `std`
|
= note: the `x86_64-unknown-uefi` target may not be installed
error[E0463]: can't find crate for `std`
|
= note: the `x86_64-unknown-uefi` target may not be installed
error: aborting due to previous error
error: aborting due to previous error
For more information about this error, try `rustc --explain E0463`.
For more information about this error, try `rustc --explain E0463`.
error[E0463]: can't find crate for `std`
|
= note: the `x86_64-unknown-uefi` target may not be installed
error: aborting due to previous error
For more information about this error, try `rustc --explain E0463`.
error: could not compile `ident_case`
To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error[E0463]: can't find crate for `std`
|
= note: the `x86_64-unknown-uefi` target may not be installed
error: aborting due to previous error
For more information about this error, try `rustc --explain E0463`.
error: build failed
My question is: why the program compiles if I don't specify a target for it? Since it is the no_std one, if it depends on std either directly or indirectly (except the proc macros), the program shouldn't compile.
Environment
cargo 1.53.0-nightly (65d57e6f3 2021-04-04)
> rustc --print cfg
debug_assertions
panic="unwind"
target_arch="x86_64"
target_endian="little"
target_env="gnu"
target_family="unix"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_has_atomic="16"
target_has_atomic="32"
target_has_atomic="64"
target_has_atomic="8"
target_has_atomic="ptr"
target_has_atomic_equal_alignment="16"
target_has_atomic_equal_alignment="32"
target_has_atomic_equal_alignment="64"
target_has_atomic_equal_alignment="8"
target_has_atomic_equal_alignment="ptr"
target_has_atomic_load_store="16"
target_has_atomic_load_store="32"
target_has_atomic_load_store="64"
target_has_atomic_load_store="8"
target_has_atomic_load_store="ptr"
target_os="linux"
target_pointer_width="64"
target_thread_local
target_vendor="unknown"
unix

As the comment from Masklinn says, the derive-builder crate indirectly depended on the ident_case, which depended on std.
This problem is fixed by this patch.

Related

Could not compile `lazy_static`

I'm having a hard time cross-compiling an embedded Rust project that worked before for a raspberry pi. I have all the needed deps in Cargo.toml but on doing:
$ cargo build --target thumbv7m-none-eabi
I get the following error.
error[E0463]: can't find crate for `std`
--> /home/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-1.4.0/src/inline_lazy.rs:9:1
|
9 | extern crate std;
| ^^^^^^^^^^^^^^^^^ can't find crate
|
= note: the `thumbv7m-none-eabi` target may not support the standard library
= help: consider building the standard library from source with `cargo build -Zbuild-std`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `lazy_static` due to previous error
$ rustup show
installed targets for active toolchain
--------------------------------------
thumbv7m-none-eabi
x86_64-unknown-linux-gnu
active toolchain
----------------
nightly-x86_64-unknown-linux-gnu (default)
rustc 1.64.0-nightly (38b72154d 2022-07-11)
Compilation used to work previously without lazy_static as a dependency in cargo.toml,now I don't understand why this is happening.
By default lazy_static depends on the rust standard library, which as the compiler told you
may not be supported on the thumbv7m-none-eabi target
If you do not need the standard library in your project you can enable the no-std feature of lazy_static like this:
lazy_static = { version = "1.5.0", features = ["spin_no_std"] }
as described here.

UseDeclaration cannot find struct in the crate root

I start with the cargo new tst. Then in the src/lib.rs I have:
pub struct Config {}
And src/main.rs looks like the following:
use crate::Config;
fn main() {}
This however does not compile:
> cargo run
Compiling tst v0.1.0 (/home/*/rust/book/tst)
error[E0432]: unresolved import `crate::Config`
--> src/main.rs:1:5
|
1 | use crate::Config;
| ^^^^^^^^^^^^^ no `Config` in the root
For more information about this error, try `rustc --explain E0432`.
error: could not compile `tst` due to previous error
But if I replace crate:: with the name of the crate like so:
use tst::Config;
fn main() {}
Then it just work:
> cargo run
Compiling tst v0.1.0 (/home/*/rust/book/tst)
warning: unused import: `tst::Config`
--> src/main.rs:1:5
|
1 | use tst::Config;
| ^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: `tst` (bin "tst") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.25s
Running `target/debug/tst`
The output for rustc --explain E0432 has the following quote, which if I understand it correctly means, that I can either use the name of a crate or simply crate:::
In Rust 2018, paths in use statements are relative to the current module
unless they begin with the name of a crate or a literal crate::, in which
case they start from the crate root. As in Rust 2015 code, the self:: and
super:: prefixes refer to the current and parent modules respectively.
Am I doing something wrong in here? Is there a way to use code from lib.rs without hardcoding the name of the crate?
> rustc --version
rustc 1.56.1 (Arch Linux rust 1:1.56.1-3)
> cat Cargo.toml
[package]
name = "tst"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
There is no way to refer to “the library crate in my package”; you must refer to it by its name. The Rust compiler doesn't (currently) know anything about other crates that happen to be in the same Cargo package; it just compiles one crate at a time.
In order for this to change, you would need to write a Rust RFC, which would need to include a description of why it would be valuable to have this feature — probably a stronger argument than “it would be less hardcoded”, since main depending on the library is almost certainly going to use library-specific names anyway, so the name of the library itself is a minor issue (unless, I suppose, you're creating many packages from a common template).

Error running cargo build with clickhouse dependency

I added this to my cargo toml file, following the instructions here
[dependencies]
clickhouse = "0.6.3"
reflection = "0.1.3"
but when I run cargo build I get a failure saying:
Compiling clickhouse v0.6.3
error[E0433]: failed to resolve: could not find `test` in `tokio`
--> /Users/gudjonragnar/.cargo/registry/src/github.com-1ecc6299db9ec823/clickhouse-0.6.3/src/compression/lz4.rs:163:10
|
163 | #[tokio::test]
| ^^^^ could not find `test` in `tokio`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0433`.
error: could not compile `clickhouse`
To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
I am quite new to Rust so I don't know what to do here, any thoughts?
I am running on MacOS BigSur if that is relevant.
I am getting this error on Linux as well. This appears to be an issue in the clickhouse crate, but it can be fixed in your Cargo.toml. #[tokio::test] refers to a macro which requires both the "rt" and "macros" features, but the Cargo.toml file in the clickhouse crate only includes the "rt" feature. In order to add this feature so that the crate will compile, you can add a line to your Cargo.toml for tokio that enables that feature:
tokio = { version = "1.0.1", features = ["rt", "macros"] }
Adding this line fixed the compiler error for me.
I noticed there is another clickhouse crate, which might also be helpful

error: can't find crate

I'm trying to use this library.
But, cargo build says this:
Compiling test v0.1.0 (file:///C:/path/to/project/test)
src\main.rs:1:1: 1:28 error: can't find crate for `jvm_assembler` [E0463]
src\main.rs:1 extern crate jvm_assembler;
^~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `test`.
To learn more, run the command again with --verbose.
My Cargo.toml is this:
[package]
name = "test"
version = "0.1.0"
authors = ["yomizu_rai"]
[dependencies]
jvm-assembler = "*"
src/main.rs is this, and there are no other sourcefiles.
extern crate jvm_assembler;
use jvm_assembler::*;
fn main() {}
I think my Cargo.toml is not wrong, and src/main.rs has no room for mistake.
Why can not rustc find jvm-assembler?
How do I resolve?
Cargo can only find crates by name if they are on crates.io. In your case you need to specify the git URL, see the section on dependencies in the Cargo documentation.

Rust alpha 1 - base64 not found

A newest version of Rust:
$ rustc --version
rustc 1.0.0-nightly (6c065fc8c 2015-02-17) (built 2015-02-18)
According to the documentation this should compile (and it compiled before):
use serialize::base64;
use serialize::base64::{ToBase64, FromBase64};
But it says
src/lib.rs:6:5: 6:22 error: unresolved import `serialize::base64`. There is no `base64` in `serialize`
src/lib.rs:6 use serialize::base64;
^~~~~~~~~~~~~~~~~
src/lib.rs:7:25: 7:33 error: unresolved import `serialize::base64::ToBase64`. Could not find `base64` in `serialize`
src/lib.rs:7 use serialize::base64::{ToBase64, FromBase64};
^~~~~~~~
src/lib.rs:7:35: 7:45 error: unresolved import `serialize::base64::FromBase64`. Could not find `base64` in `serialize`
src/lib.rs:7 use serialize::base64::{ToBase64, FromBase64};
^~~~~~~~~~
error: aborting due to 3 previous errors
You are using the nightlies, and so should refer to the nightlies documentation.
The serialize crate as been moved to an external repository on crates.io to use it, simply insert
[dependencies]
rustc-serialize = "0.2"
in you Cargo.toml and import the crate in your rust code with:
extern crate "rustc-serialize" as rustc_serialize;
use rustc_serialize::base64;
You should also change your #[derive(Encodable)] and #[derive(Decodable)] to #[derive(RustcEncodable)] and #[derive(RustcDecodable)]

Resources