【问题标题】:cannot perform const operation using `T`无法使用 `T` 执行 const 操作
【发布时间】:2022-09-23 07:28:12
【问题描述】:

我是 Rust 的几个小时新手。 我正在尝试从https://docs.rs/docx/latest/docx/ 编译示例代码以使用 docx crate。

这是示例代码:

use docx::document::Paragraph;
use docx::DocxFile;

let docx = DocxFile::from_file(\"origin.docx\").unwrap();
let mut docx = docx.parse().unwrap();

let para = Paragraph::default().push_text(\"Lorem Ipsum\");
docx.document.push(para);

docx.write_file(\"origin_appended.docx\").unwrap();

我编辑了我的 Cargo.toml 以包含 docx 依赖项。 这是我得到的完整错误:

   Compiling bzip2-sys v0.1.11+1.0.8
   Compiling jetscii v0.4.4
   Compiling quote v1.0.21
   Compiling time v0.1.44
error: generic parameters may not be used in const operations
   --> /home/thwart/.cargo/registry/src/github.com-1ecc6299db9ec823/jetscii-0.4.4/src/simd.rs:109:13
    |
109 |             T::CONTROL_BYTE,
    |             ^^^^^^^^^^^^^^^ cannot perform const operation using `T`
    |
    = note: type parameters may not be used in const expressions

error: generic parameters may not be used in const operations
   --> /home/thwart/.cargo/registry/src/github.com-1ecc6299db9ec823/jetscii-0.4.4/src/simd.rs:148:13
    |
148 |             T::CONTROL_BYTE,
    |             ^^^^^^^^^^^^^^^ cannot perform const operation using `T`
    |
    = note: type parameters may not be used in const expressions

error: could not compile `jetscii` due to 2 previous errors

为什么 Rust 编译 jetscii?如何修复此错误? 谢谢!

  • 您使用的是哪个版本的 Rust?
  • rustc -V = 1.62.1
  • @PitaJ 那是jetscii 的旧版本。可能是docx 应该更新到新版本。虽然总的来说这对我来说很奇怪,因为 Rust 编译器不应该破坏曾经编译过的代码。
  • 所以看起来问题出在 \'docx\' crate 而不是上面的示例代码。我将 docx 作为依赖项添加到我的 hello-world 项目中并得到了同样的错误。
  • @ed_is_my_name 是的,我想。编译器错误出现在 docx crate 的编译中,而不是您的代码中。

标签: rust cargo


【解决方案1】:

这是由稍微不兼容的 Rust 更新和旧 crate 的不幸组合造成的。 docx 库通过这个依赖树依赖于 jetscii:

docx v1.1.2
└── strong-xml v0.5.0
    └── jetscii v0.4.4

您可以查看jetscii issue #56jetscii pull request #39rust-lang/stdarch issue #248 了解更多信息,但基本摘要是(由 shepmaster 提供):

TL;DR,旧版本的 Rust 以一种方式暴露了 SIMD 内在函数,这种方式被改变了,并且一小部分 crate(比如 Jetscii)受到了影响。

jetscii 和 strong-xml 早已更新以适应这种变化,但 docx 没有(两年多没有更新)。

要解决此问题,您的选择是有限的:

  • 使用 Rust 1.53 版
  • 自己使用更新的依赖项更新 docx
  • 更改库(考虑docx-rsdocx-rust

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多