在 Rust 中指定或导入 Signed 特征的推荐方法是什么?

What is the recommended way to specify or import the Signed trait in Rust?

Signed 特征 used to exist in the standard library, but it's gone now. A year ago, someone asked about implementing signed traits on reddit, but the thread didn't come to a conclusion, and the author moved on to ask again on the Rust language Discourse site with similar results

现在 num crate 中有一个 Signed 特征,但我有两个问题:

1) 我真的不明白将 crate 导入我的项目是否会导致文件大小的开销更大,尤其是当我只使用它的一个特征时。

2) 考虑到这种特性的明显不稳定性,如果它不能保持稳定和兼容,我现在对在我的项目中添加另一个依赖持怀疑态度。

在这里绘制图表的正确路线是什么?

使用箱子。编译器将确保创建一个高效的二进制文件。您可能不会注意到文件大小有任何有趣的变化。 Cargo 将确保实际使用板条箱很容易。

特征的 "stability" 不是特征的固有 属性。大多数情况下,该特征的 有用性 值得怀疑。标准库必须是保守的——任何存在的东西都必须在 Rust 1.x 的整个生命周期内得到支持(并且没有 Rust 2.x 的计划).因此,向标准库添加内容非常受限。

这就是为什么板条箱是第一个 class 公民。现在由 num crate 的维护者决定支持该特性多长时间。如果他们遵循 semver(他们应该),那么他们是否会放弃对它的支持应该是非常明显的。此外,一旦您选择了要编译的 crate 版本,它将永远不会改变,直到您执行 cargo update,因此您可以安全地使用它。

在最坏的情况中,假设 num crate 决定放弃该特征。在那种情况下, 可以简单地复制实现并生成你自己的 crate。