Rust 对特定包的混淆

问题描述 投票:0回答:1

使用 Rust 的第一步。我正在尝试按照一段相当简单的代码here来分析.docx文件(MS Word文件)的内容。

该页面的底部是假定已完成的代码(我还按照说明添加到 Cargo.toml 的“依赖项”部分)。

但它不起作用:

error: no matching package found
searched package name: `docx_rs`
perhaps you meant:      docx-rs
location searched: registry `crates-io`

...所以,有点困惑,我把“docx-rs”放在 Cargo.toml 中,也在 main.rs 中(

use docx-rs::*;
)。这产生了“找到不匹配的候选版本”...所以我编辑了 Cargo.toml 来制作最新版本(如有用消息所建议的)0.4.7。这给出了 2 个错误:

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

怀疑这确实是错误的crate...此时我去查看docx_rs(下划线)是否存在。是的:这里。目前的版本据说是...0.4.7。令人毛骨悚然。

这是我明确要“安装”到我的系统中的“外部”板条箱吗?谁能解释我做错了什么?

NB“docx-rs”(连字符而不是下划线)似乎也存在这里。据说最后一个版本是“0.2.0”。

稍后

按照 at54321 的建议进行操作后,我现在似乎收到了有关另一个板条箱的投诉。

   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`

根据脚本,这里的

dependencies
行是
clap = "2.33.0"
use
行是
use clap::Parser;

rust rust-cargo rust-crates
1个回答
4
投票

外部(第三方)板条箱需要通过 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(带连字符),您也必须导入它并将其引用为

docx_rs
(带下划线)。这是因为 Rust/Cargo 中连字符会自动转换为下划线。

© www.soinside.com 2019 - 2024. All rights reserved.