IeuD
IeuD

Reputation: 83

Why building with rustc command cannot see crates?

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 <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
glob = "0.3.0"

Upvotes: 2

Views: 927

Answers (1)

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160457

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.

Upvotes: 2

Related Questions