First steps with Rust. I'm trying to following a fairly simple piece of code here to analyse the contents of .docx files (MS Word files).
At the bottom of that page is the supposed completed code (I also added to the "dependencies" section of Cargo.toml as instructed).
But it doesn't work:
error: no matching package found
searched package name: `docx_rs`
perhaps you meant: docx-rs
location searched: registry `crates-io`
... so, slightly puzzled, I put "docx-rs" instead in Cargo.toml, and also in main.rs (use docx-rs::*;
). This produced "candidate versions found which didn't match"... so I edited Cargo.toml to make the newest version (as suggested by the helpful message), 0.4.7. This gives 2 errors:
error: expected one of `::`, `;`, or `as`, found `-`
--> src\main.rs:31:13
|
31 | use docx-rs::*;
| ^ expected one of `::`, `;`, or `as`
error[E0432]: unresolved import `clap::Parser`
--> src\main.rs:30:9
|
30 | use clap::Parser;
| ^^^^^^^^^^^^ no `Parser` in the root
Suspicion that this is indeed the wrong crate... At this point I went looking to see whether docx_rs (underscore) exists. Yes: here. Current version said to be ... 0.4.7. Spooky.
Is this an "external" crate which I have explicitly to "install" into my system? Can anyone explain what I'm doing wrong?
NB "docx-rs" (hyphen not underscore) also appears to exist here. Last release said to be "0.2.0".
Later
After doing what at54321 suggested, I'm now seemingly getting complaints about another crate.
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:35:14
|
35 | #[derive(Parser, Debug)]
| ^^^^^^
|
= note: import resolution is stuck, try simplifying macro imports
error: cannot find attribute `command` in this scope
--> src\main.rs:36:7
|
36 | #[command(author, version, about, long_about = None)]
| ^^^^^^^
error: cannot find attribute `arg` in this scope
--> src\main.rs:38:11
|
38 | #[arg(short, long)]
| ^^^
error[E0599]: no function or associated item named `parse` found for struct `main::Args` in the current scope
--> src\main.rs:69:26
|
37 | struct Args {
| ----------- function or associated item `parse` not found for this struct
...
69 | let args = Args::parse();
| ^^^^^ function or associated item not found in `main::Args`
As per script, the dependencies
line here is clap = "2.33.0"
and the use
line use clap::Parser;
.
External (third-party) crates need to be added to your project via Cargo. One way to do that is to run this under your project's folder:
cargo add docx-rs
As a result, in your Cargo.toml
Cargo will simply add a line like this to your [dependencies]
section:
docx-rs = "0.4.7"
As an alternative, you could manually add that line to your Cargo.toml
. In fact cargo add
wasn't available until recently, so manually editing Cargo.toml
was the only option to add a dependency to your project.
Even though the name of the package in https://cargo.io is docx-rs (with a hyphen), you must import it and refer to it as docx_rs
(with an underscore). That's because hyphens are automatically converted to underscores in Rust/Cargo.