关于特定包的Rust混淆

huangapple go评论59阅读模式
英文:

Rust confusion about specific package

问题

以下是翻译好的部分:

"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;.

英文:

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;.

答案1

得分: 4

外部(第三方)crate 需要通过 Cargo 添加到您的项目中。其中一种方法是在您的项目文件夹下运行以下命令:

cargo add docx-rs

结果,在您的 `Cargo.toml` 文件中,Cargo 将简单地在您的 `[dependencies]` 部分添加如下行:

docx-rs = "0.4.7"

作为一种替代方案,您可以手动将该行添加到您的 `Cargo.toml` 中。事实上,直到最近,`cargo add` 都不可用,因此手动编辑 `Cargo.toml` 是将依赖项添加到项目中的唯一选项。

尽管在 https://cargo.io 上包的名称是 [docx-rs](https://crates.io/crates/docx-rs)(带连字符),但您必须将其导入并引用为 `docx_rs`(带下划线)。这是因为在 Rust/Cargo 中连字符会自动转换为下划线。
英文:

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.

huangapple
  • 本文由 发表于 2023年7月7日 03:42:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632095.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定