Rust alpha 1 - base64 not found - rust

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

Related

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).

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

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.

Unable to run a simple example -- "use of unstable library feature 'rustc_private': ....."

I'm trying to use simplelog.rs in my Rust application. The hello world example
#[macro_use]
extern crate log;
extern crate simplelog;
// ..........
CombinedLogger::init(vec![
TermLogger::new(LogLevelFilter::Warn, simplelog::Config::default()).unwrap(),
WriteLogger::new(LogLevelFilter::Info, simplelog::Config::default(), File::create("log.log").unwrap())]).unwrap();
results into this:
error: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead? (see issue #27812)
--> src/main.rs:9:1
|
9 | extern crate log;
| ^^^^^^^^^^^^^^^^^
error: aborting due to previous error
How to fix it?
From the discussion of #27812 it's still unclear what to do about it.
You're missing log = "version" entry in Cargo.toml [dependencies].
Because of that Cargo doesn't give Rust a log crate to load with extern crate log, and Rust keeps looking for it deeper, finding some internal one.

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.

Unable to find symbols from extern crates included with `use`

I'm trying to use some Rust libraries from crates on Github. This is the first time I've tried to do this. The code, lifted from an "html" library example, begins like this:
mod interactive_test {
extern crate http;
extern crate url;
use std::os;
use std::str;
use url::Url;
use http::client::RequestWriter;
use http::method::Get;
use http::headers::HeaderEnum;
// ...
}
fn main() {}
Errors look like this:
error[E0432]: unresolved import `url::Url`
--> src/main.rs:7:9
|
7 | use url::Url;
| ^^^^^^^^ Did you mean `self::url`?
error[E0432]: unresolved import `http::client::RequestWriter`
--> src/main.rs:9:9
|
9 | use http::client::RequestWriter;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean `interactive_test::http`?
error[E0432]: unresolved import `http::method::Get`
--> src/main.rs:10:9
|
10 | use http::method::Get;
| ^^^^^^^^^^^^^^^^^ Did you mean `self::http::method`?
error[E0432]: unresolved import `http::headers::HeaderEnum`
--> src/main.rs:11:9
|
11 | use http::headers::HeaderEnum;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean `interactive_test::http`?
The Cargo.toml file contains
[dependencies.http]
http = "https://github.com/chris-morgan/rust-http"
[dependencies.url]
url = "0.2.7"
and the HTTP and URL packages were found and fetched by cargo build earlier.
The extern crate http and extern crate url lines do not generate errors; the crates are being found by the compiler, but those crates don't seem to contain the expected symbols. If I add `extern crate foo", I get an error, so that is checked.
This is probably some problem with how Rust or Cargo search for libraries. Rust is installed in ~/local, not as root, done by setting the --prefix parameter during installation. That may have broken something, although Cargo should handle that. Basic stuff like "hello_world" works fine; bringing in external libraries does not.
I notice that cargo update doesn't cause a re-fetch of the http and url crates from Github. The documentation indicates that it should.
Versions:
Ubuntu 14.04 LTS.
rustc 0.13.0-nightly (96a3c7c6a 2014-12-23 22:21:10 +0000)
cargo 0.0.1-pre-nightly (e11c317 2014-12-21 20:43:45 +0000)
The compiler gave you the answer you need.
Your extern crate statements are inside a module, and use statements require absolute paths. That is, when you say use url::Url; inside the interactive_test module, what you are actually saying is "use url::Url which is defined in the root module", which it isn't.
What you need to do is prefix the path with self:: to tell it to look in the current module. You can also use super:: to access the parent module (if that ever comes up).
Personally, I get around this by putting all my extern crate statements in the root module, which also serves as a kind of program-wide list of external crates being used.

Resources