Why building with rustc command cannot see crates? - rust

Hi I'm trying to explore Rust. What I'm trying to do is using glob module, when i build code with $cargo build and $cargo run it's succesfully builds and runs the executable but if i try it with $rustc main.rs it gives me
error[E0432]: unresolved import `glob`
--> src/main.rs:1:5
|
1 | use glob::glob;
| ^^^^ use of undeclared type or module `glob`
Any ideas?
Version : ╰─ rustc --version rustc 1.43.1 (8d69840ab 2020-05-04)
Code is here:
use glob::glob;
fn main() {
for entry in glob("/home/user/*.jpg").unwrap(){
match entry {
Ok(path) => println!("{:?}", path.display()),
Err(e) => println!("{:?}",e),
}
};
}
My toml
[package]
name = "test1"
version = "0.1.0"
authors = ["ieuD <example#mail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
glob = "0.3.0"

rustc on its own doesn't handle your dependencies, it just compiles stuff. When you run rustc on your file, it will start compiling it and encounter the unknown glob crate. Dependencies are handled by cargo via Cargo.toml.
Though you could only use rustc (see the answer here) it's a task that's considerably more difficult, that's why cargo is around.

Related

Unable to compile Rust AWS Lambda using rusoto

Im having some problems compiling a Rust lambda. The problems started after I included the rusoto package, and it is complaining about linker problems (options and libraries).
Im running:
cargo lambda build
Getting:
= note: warning: unsupported linker arg: -znoexecstack
warning: unsupported linker arg: -zrelro
warning: unsupported linker arg: -znow
ld.lld: error: unable to find library -lssl
ld.lld: error: unable to find library -lcrypto
My Cargo.toml:
[package]
name = "super-lambda"
version = "0.1.0"
edition = "2021"
# Starting in Rust 1.62 you can use `cargo add` to add dependencies
# to your project.
#
# If you're using an older Rust version,
# download cargo-edit(https://github.com/killercup/cargo-edit#installation)
# to install the `add` subcommand.
#
# Running `cargo add DEPENDENCY_NAME` will
# add the latest version of a dependency to the list,
# and it will keep the alphabetic ordering for you.
[dependencies]
lambda_http = "0.7"
lambda_runtime = "0.7"
lazy_static = "1.4.0"
rusoto_core = "0.48.0"
rusoto_dynamodb = "0.48.0"
serde = "1.0.149"
tokio = { version = "1", features = ["macros"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
I did try to disable the zig-linker:
cargo lambda build --disable-zig-linker
Then it compiles, but AWS will complain:
/var/task/bootstrap: error while loading shared libraries: libssl.so.3: cannot open shared object file: No such file or directory
What can I do to resolve this?
Cargo-lambda will do a cross-compilation targeting x86_64-unknown-linux-gnu. To avoid relying on a libssl dependency, you can try building the project using rustls for rusoto. You should be able to enable rustls feature for rusoto as follows:
rusoto_core = {version = "0.48.0", features = ["rustls"], default-features = false}
rusoto_dynamodb = {version = "0.48.0", features = ["rustls"], default-features = false}
You can also see this GitHub issue which relates to this topic.
If you are on Linux, you might try to install libssl-dev and try to build it again. However, in my experience, zigbuild (used by cargo-lambda for cross-compilation) might struggle to find this dependency.

Compiling bevy_dylib v0.5.0 error: linking with `cc` failed: exit status: 1

On Mac freshly upgraded to Monterey, I'm getting the following when attempting to cargo run a trivial Bevy program. I've reinstalled XCode CLTs like recommended here and other places. I've tried messing around with some of the cargo.yml with no success.
Compiling bevy_dylib v0.5.0
error: linking with `cc` failed: exit status: 1
note: "cc" "-Wl,-exported_symbols_list,/var/folders/rp/lky8r76j5v5dk0rqg_yzk35w0000gn/T/rustcDYBmaq/list"
"-m64" "-arch" "x86_64" <......... many pages of warnings>
ld: warning: object file (/Users/BWStearns/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy-glsl-to-spirv-0.2.1/build/osx/libSPIRV-Tools-opt.glsltospirv.a(const_folding_rules.cpp.o)) was built for newer macOS version (10.13) than being linked (10.7)
<many more pages of warnings>
"_CGDisplayCreateUUIDFromDisplayID", referenced from:
_$LT$winit..platform_impl..platform..monitor..MonitorHandle$u20$as$u20$core..cmp..PartialEq$GT$::eq::h541b069daf520ec9 in libwinit-4f8b93ad49cc21f8.rlib(winit-4f8b93ad49cc21f8.winit.355ded65-cgu.6.rcgu.o)
_$LT$winit..platform_impl..platform..monitor..MonitorHandle$u20$as$u20$core..cmp..Ord$GT$::cmp::h951d61f6bd1d7a5f in libwinit-4f8b93ad49cc21f8.rlib(winit-4f8b93ad49cc21f8.winit.355ded65-cgu.6.rcgu.o)
winit::platform_impl::platform::monitor::MonitorHandle::ns_screen::h3207f8aed4eae22f in libwinit-4f8b93ad49cc21f8.rlib(winit-4f8b93ad49cc21f8.winit.355ded65-cgu.6.rcgu.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: could not compile `bevy_dylib` due to previous error
The cargo.yml is like this:
[package]
name = "my_bevy_game"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = { version = "0.5.0", features = ["dynamic"] }
I just ran into this starting this week. I am pretty sure this exact code was compiling fine a couple weeks ago. Any help would be greatly appreciated.
main.rs looks like
use bevy::prelude::*;
struct Person;
struct Name(String);
fn add_people(mut commands: Commands) {
commands.spawn().insert(Person).insert(Name("Elaina Proctor".to_string()));
commands.spawn().insert(Person).insert(Name("Renzo Hume".to_string()));
commands.spawn().insert(Person).insert(Name("Zayna Nieves".to_string()));
}
fn greet_people(query: Query<&Name, With<Person>>) {
for name in query.iter() {
println!("hello {}!", name.0);
}
}
pub struct HelloPlugin;
impl Plugin for HelloPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_startup_system(add_people.system())
.add_system(greet_people.system());
}
}
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_plugin(HelloPlugin)
.run();
}
Update:
Definitely something about the environment on my machine because on a different macbook it worked fine.
So I found a solution if you discover this issue and the code works on other machines. Uninstall rust with rustup self uninstall and then renistall with the standard script curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh and then you should be good.

use of undeclared crate or module in rust [duplicate]

If I define a dependency like foo = { version = "1.0.0", optional = true },
will it be available when I do "cargo run"? Can I check if it is enabled in the code?
if cfg!(feature = "foo") {}
Doesn't seem to be working, like the feature is missing all the time.
Moving answer to 60258216 here:
Optional dependencies do double as features: https://stackoverflow.com/a/39759592/8182118
They will not be enabled by default unless they're listed in the default feature, though you can enable the feature using cargo run --features foo.
For clarity and forward compatibility you may want to create an actual feature which enables the dependency though, that way if you need to "fluff up" the feature in the future and that requires new optional dependencies it's much easier.
In the code, both #[cfg] and cfg! should work depending whether you want to check for it at compile-time or runtime.
It's not hard to test either:
[package]
name = "testx"
version = "0.1.0"
edition = "2018"
[features]
default = ["boolinator"]
magic = ["boolinator"]
empty = []
[dependencies]
boolinator = { version = "*", optional = true }
and a main.rs of:
fn main() {
# macro and attributes would work the same here
if cfg!(feature = "boolinator") {
println!("Hello, bool!");
} else {
println!("Hello, world!");
}
}
you get
$ cargo run -q
Hello, bool!
$ cargo run -q --no-default-features
Hello, world!
$ cargo run -q --no-default-features --features boolinator
Hello, bool!
$ cargo run -q --no-default-features --features magic
Hello, bool!
$ cargo run -q --no-default-features --features empty
Hello, world!
See also https://github.com/rust-lang/edition-guide/issues/96

"Failed to parse manifest" when compiling rustc using a locally-modified copy of the libc crate

I need to build the rustc compiler using a modified libc crate. I cloned the libc directory and made the changes, now how do I include the modified libc in my build?
This is my Cargo.toml
[patch.crates-io]
# Similar to Cargo above we want the RLS to use a vendored version of `rustfmt`
# that we're shipping as well (to ensure that the rustfmt in RLS and the
# `rustfmt` executable are the same exact version).
rustfmt-nightly = { path = "src/tools/rustfmt" }
# See comments in `src/tools/rustc-workspace-hack/README.md` for what's going on
# here
rustc-workspace-hack = { path = 'src/tools/rustc-workspace-hack' }
# See comments in `tools/rustc-std-workspace-core/README.md` for what's going on
# here
rustc-std-workspace-core = { path = 'src/tools/rustc-std-workspace-core' }
rustc-std-workspace-alloc = { path = 'src/tools/rustc-std-workspace-alloc' }
rustc-std-workspace-std = { path = 'src/tools/rustc-std-workspace-std' }
libc = {path = "../libc"}
[patch."https://github.com/rust-lang/rust-clippy"]
clippy_lints = { path = "src/tools/clippy/clippy_lints" }
[dependencies]
# libc = {verion = "0.2", default-features= false, path = "../libc"}
This is the error I get:
mahto#hydlnxeng27:/local/mnt/workspace/mahto/rust$ ./x.py build --config config.toml src/libstd 2>&1 | tee build.log
Updating only changed submodules
Submodules updated in 0.04 seconds
error: failed to parse manifest at `/local/mnt/workspace/mahto/rust/Cargo.toml`
Caused by:
virtual manifests do not specify [dependencies]
failed to run: /local/mnt/workspace/mahto/rust/build/x86_64-unknown-linux-gnu/stage0/bin/cargo build --manifest-path /local/mnt/workspace/mahto/rust/src/bootstrap/Cargo.toml
Build completed unsuccessfully in 0:00:00
After commenting-out the dependencies section in Cargo.toml, I get this new error:
error[E0433]: failed to resolve: unresolved import
error: aborting due to previous error
For more information about this error, try `rustc --explain E0433`.
error: could not compile `libc`.

My Cargo.toml is displaying some red lines with error couldn't compile serde_derive

I try to build a project using rust-lang recently (this is my first rust project, and my boss is supporting me to use a new technology in my company). But, I suddenly get some red lines on my Cargo.toml :
could not compile `serde_derive`.
error: could not compile `async-trait`.
To learn more, run the command again with --verbose.
error: could not compile `rand_chacha`.
To learn more, run the command again with --verbose.
error: could not compile `proc-macro-hack`.
To learn more, run the command again with --verbose.
error: could not compile `diesel_derives`.
To learn more, run the command again with --verbose.
I run a command Cargo Run and my project is running well, but these red lines prevent me to tracking an error on my other code in my project (So if there is an error in code, it won't display as there are still exists some errors in another file, it is Cargo.toml)
I am using cargo 1.43.0-nightly (bda50510d 2020-03-02), rustc 1.43.0-nightly (c20d7eecb 2020-03-11), and vs code 1.43 version.
This is my Cargo.toml :
[package]
name = "app_base"
version = "0.1.0"
authors = ["yonathan"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.3"
rocket_codegen = "0.4.3"
rocket_contrib = "0.4.3"
rocket_http = "0.4.3"
cookie = "0.11.2"
rocket-json-response = "0.5.10"
diesel = { version = "1.4.3", features = ["postgres"] }
dotenv = "0.15.0"
postgres = { version = "0.17.2", features = ["with-chrono-0_4"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
json-gettext = "3.1.7"
debug-helper = "0.3.8"
serializers = "0.2.3"
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", branch = "master" }
chrono = "0.4"
As this is an RLS (rust language server) bug [which is closed apparently, see here], a temporary alternative can be using the rust-analyzer extension [website here].
To install the extension, you can launch VSCode, click on the Extensions tab on the left and search for rust-analyzer in the Marketplace.
Please note that as mentioned on the website [as of 04/01/2020], this project is still in ALPHA, which means it may be prone to breakages.

Resources