0

Still trying to run this script here. And still a Rustacean learner.

Yesterday I had a problem with the docx_rs/docx-rs crate. Enlightenment from at54321.

But the script still doesn't run. It appears to have a problem with a crate called clap.

D:\My documents\...\word_file>cargo run
   Compiling word_file v0.1.0 (D:\My documents\software projects\EclipseWorkspace\py_exp\src\rust_2023-07A\word_file)
error[E0432]: unresolved import `clap::Parser`
  --> src\main.rs:30:9
   |
30 |     use clap::Parser;
   |         ^^^^^^^^^^^^ no `Parser` in the root

error: cannot determine resolution for the derive macro `Parser`
  --> src\main.rs:36:14
   |
36 |     #[derive(Parser, Debug)]
   |              ^^^^^^
   |
   = note: import resolution is stuck, try simplifying macro imports

error: cannot find attribute `command` in this scope
  --> src\main.rs:37:7
   |
37 |     #[command(author, version, about, long_about = None)]
   |       ^^^^^^^

error: cannot find attribute `arg` in this scope
  --> src\main.rs:39:11
   |
39 |         #[arg(short, long)]
   |           ^^^

error[E0599]: no function or associated item named `parse` found for struct `main::Args` in the current scope
  --> src\main.rs:71:26
   |
38 |     struct Args {
   |     ----------- function or associated item `parse` not found for this struct
...
71 |         let args = Args::parse();
   |                          ^^^^^ function or associated item not found in `main::Args`

Very impressed with the clarity of Rust feedback. But I haven't managed to understand what's actually wrong here. As per the linked script my dependencies section of Cargo.toml includes this line:

clap = "2.33.0"

... anyone know what's wrong this time?

Later

After changing to clap = "4.3.11". Still errors:

error: cannot find derive macro `Parser` in this scope
  --> src\main.rs:36:14
   |
36 |     #[derive(Parser, Debug)]
   |              ^^^^^^
   |
note: `Parser` is imported here, but it is only a trait, without a derive macro
  --> src\main.rs:30:9
   |
30 |     use clap::Parser;
   |         ^^^^^^^^^^^^

... + 3 more errors no doubt due to the above

Unfortunately I haven't got to that part of Rust 101 where you learn about "derive macros"...! I have no idea what that means.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • 2.33 is quite an old version of clap (https://crates.io/crates/clap/versions). What happens if you install the latest version? – richzilla Jul 07 '23 at 14:57
  • Thanks. See update. But the thing is, one might expect that script to run even with that old version. As you can see, the updated version also causes a fail, seemingly the same fail, "derive macro"... – mike rodent Jul 07 '23 at 15:03
  • 1
    To use derive macros, you'll need to enable the 'derive' feature of the library. Its not shown on the crates.io page, but is in the documentation: https://docs.rs/clap/latest/clap/_features/index.html. Try running `cargo add clap --features derive` – richzilla Jul 07 '23 at 15:06
  • Thanks. Success! Is "features" and/or "derive" a thing across lots of crates? No doubt I'll find out in due course. – mike rodent Jul 07 '23 at 15:15
  • 2
    Features are a common concept. There's nothing inherently special about the word 'derive' (its just the name of the feature), but quite a lot of crates that use macros (the things in the square brackets) will call the feature needed to enable them 'derive'. – richzilla Jul 07 '23 at 15:18
  • 1
    I think the original blog/script was mistaken, there is no `Parser` or `"derive"` feature flag in Clap 2.33. That functionality was only added in version 3.0. – kmdreko Jul 07 '23 at 15:19

1 Answers1

1

(this answer added because question needs to be answered but not necessarily closed)

As stated by richzilla, the answer in this case is to use the latest (stable) version of "clap" and to run this command first:

cargo add clap --features derive

"Features" being, as stated, a common concept in crates.

mike rodent
  • 14,126
  • 11
  • 103
  • 157