2

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"

IeuD
  • 83
  • 6
  • 1
    You do have to tell rustc where to find the external crate (that you've already built) and also the library path where the dependencies of that crate can be found. What is your rustc command line? – Jussi Kukkonen May 24 '20 at 14:31
  • @JussiKukkonen my command was straight rustc src/main.rs. for clarification i've added glob dependency to toml file. [glob crate](https://crates.io/crates/glob) and built with cargo. how can i specify the crate path. would you give me an example ? – IeuD May 24 '20 at 15:00
  • You added a dependency to **Cargo**.toml file, a configuration file for **Cargo**... If you want to see an example of working rustc calls, run "cargo clean && cargo -v build" to see the commands cargo runs. https://doc.rust-lang.org/rustc lists the command line options. – Jussi Kukkonen May 24 '20 at 16:39

1 Answers1

2

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.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253