关于特定包的Rust混淆

64jmpszr  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(94)

第一步与 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


怀疑这确实是错误的板条箱...在这一点上,我去看看docx_rs(下划线)是否存在。是的:here。当前版本据说是... 0.4.7,好恐怖
这是我必须明确“安装”到我的系统中的“外部”机箱吗?谁能解释一下我做错了什么?
NB“docx-rs”(连字符而非下划线)似乎也存在here。上一个版本称为“0. 2. 0”。

  • 稍后 *

在做了at 54321建议的事情之后,我现在似乎收到了关于另一个板条箱的投诉。

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;

vs91vp4v

vs91vp4v1#

外部(第三方)板条箱需要通过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中会自动转换为下划线。

相关问题