简体   繁体   English

`bigdecimal::BigDecimal`,它没有实现 `Copy` 特性

[英]`bigdecimal::BigDecimal`, which does not implement the `Copy` trait

I need to use the bigdecimal crate, so I include it in my dependencies in Cargo.toml file:我需要使用bigdecimal crate,所以我将它包含在Cargo.toml文件中的依赖项中:

[dependencies]
bigdecimal = "0.1.0"

While writing the code, I get the following error:在编写代码时,我收到以下错误:

bigdecimal::BigDecimal, which does not implement the 'Copy' trait

For example:例如:

use bigdecimal::BigDecimal;
use std::str::FromStr;

fn main() {
    let val_1 = BigDecimal::from_str("1").unwrap();
    let val_2 = BigDecimal::from_str("2").unwrap();

    let result = val_2/val_1;

    println!("Test 1 {} ", result);
    println!("Test 2 {} ", val_2);
}

When execute the error message below will appear:执行时会出现以下错误信息:

----- move occurs because `val_2` has type `bigdecimal::BigDecimal`, which does not implement the `Copy` trait

The only way I can solve it is by declaring again val_2 before the print statement我可以解决它的唯一方法是在打印语句之前再次声明 val_2

println!("Test 1 {} ", result);
let val_2 = BigDecimal::from_str("2").unwrap();
println!("Test 2 {} ", val_2);

Is there any other efficient way to solve it?有没有其他有效的方法来解决它?

In this case you can just take references:在这种情况下,您可以参考:

let result = &val_2/&val_1;

to use the proper version of division: impl<'a, 'b> Div<&'b BigDecimal> for &'a BigDecimal where your code currently uses impl Div<BigDecimal> for BigDecimal (you can find both in https://docs.rs/bigdecimal/0.1.0/bigdecimal/struct.BigDecimal.html as well as many other versions).使用正确版本的除法: impl<'a, 'b> Div<&'b BigDecimal> for &'a BigDecimal您的代码当前使用impl Div<BigDecimal> for BigDecimal (您可以在https:// docs.rs/bigdecimal/0.1.0/bigdecimal/struct.BigDecimal.html以及许多其他版本)。 If the difference doesn't make sense to you, you really need to read References and Borrowing first (and the 'a 'b parts can probably be ignored for now).如果差异对您没有意义,您真的需要先阅读参考文献和借用书(现在可能可以忽略'a 'b部分)。

In other cases, you may need calls like val2.clone() to make a copy explicitly.在其他情况下,您可能需要像val2.clone()这样的调用来显式地进行复制。

For why BigDecimal can't be Copy (then it would make copies automatically whenever needed), see https://github.com/rust-num/num/issues/191 :关于为什么BigDecimal不能被Copy (然后它会在需要时自动复制),请参阅https://github.com/rust-num/num/issues/191

As a rule of thumb, only primitives and aggregates composed of just primitives can implement Copy .根据经验,只有原语和仅由原语组成的聚合才能实现Copy So for instance, PrimInt does implement Copy , but BigInt has a Vec underneath, so it can only be explicitly cloned.例如, PrimInt确实实现了Copy ,但BigInt在下面有一个Vec ,所以它只能被显式克隆。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 特性 `diesel::Expression` 没有为 `bigdecimal::BigDecimal` 实现 - the trait `diesel::Expression` is not implemented for `bigdecimal::BigDecimal` 移动发生是因为值的类型为 Vec<T> ,它没有实现 `Copy` 特性 - move occurs because value has type Vec<T>, which does not implement the `Copy` trait 在匹配中分配向量的值:移动发生......它没有实现“复制”特征 - Assigning value of vector inside match: move occurs … which does not implement the `Copy` trait Rust:发生移动是因为类型为 `ReadDir`,它没有实现 `Copy` 特征 - Rust: move occurs because has type `ReadDir`, which does not implement the `Copy` trait 由于 `slice[_]` 的类型为 `T`,它没有实现 `Copy` 特征,因此无法移出此处发生移动 - Cannot move out of here move occurs because `slice[_]` has type `T`, which does not implement the `Copy` trait move 发生是因为 value 的类型为 `RefCell&lt;…&gt;`,它没有实现 `Copy` 特征 - move occurs because value has type `RefCell<…>`, which does not implement the `Copy` trait 如何避免“由于`v`的类型为`Vec而发生移动<char> `,它没有在循环中实现`Copy` trait&quot; - How to avoid "move occurs because `v` has type `Vec<char>`, which does not implement the `Copy` trait" within loop 发生 Rust 文件树移动是因为 `subtree` 的类型为 `trees::Tree<PathBuf> `,它没有实现 `Copy` 特征 - Rust File Tree move occurs because `subtree` has type `trees::Tree<PathBuf>`, which does not implement the `Copy` trait 为另一个特征实现一个特征是什么意思? - What does it mean to implement a trait for another trait? 所有原始类型都实现了复制特征吗? - Do all primitive types implement the Copy trait?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM