简体   繁体   English

移动发生是因为值的类型为 Vec<T> ,它没有实现 `Copy` 特性

[英]move occurs because value has type Vec<T>, which does not implement the `Copy` trait

I am writing a very simple recursive program for finding all prime numbers between two numbers:我正在编写一个非常简单的递归程序,用于查找两个数字之间的所有质数:

use std::cmp::PartialOrd;
use std::ops::{Add, Div, Rem, Sub};

fn _is_prime<T>(n: T, dividend: T, one: T) -> bool
where
    T: Copy + Rem<Output = T> + Sub<Output = T> + PartialOrd,
{
    if dividend == one {
        true
    } else {
        if n % dividend < one {
            false
        } else {
            _is_prime(n, dividend - one, one)
        }
    }
}

fn _primes_between<'a, T>(a: T, b: T, one: T, v: &'a mut Vec<T>) -> &'a mut Vec<T>
where
    T: Copy + Rem<Output = T> + Add<Output = T> + Sub<Output = T> + PartialOrd,
{
    if a <= b {
        if _is_prime(a, a - one, one) {
            v.push(a);
        }

        _primes_between(a + one, b, one, v)
    } else {
        v
    }
}

fn primes_between<T>(a: T, b: T) -> Vec<T>
where
    T: Copy + Div<Output = T> + Rem<Output = T> + Add<Output = T> + Sub<Output = T> + PartialOrd,
{
    let one = a / a;

    let mut v: Vec<T> = Vec::new();

    *_primes_between(a, b, one, &mut v)
}

fn main() {
    primes_between(3, 13).iter().for_each(|i| println!("{}", i));
}

The problem is:问题是:

error[E0507]: cannot move out of a mutable reference
  --> src/main.rs:42:5
   |
42 |     *_primes_between(a, b, one, &mut v)
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `std::vec::Vec<T>`, which does not implement the `Copy` trait

How do I solve that error?我该如何解决该错误?

I'm not 100% sure, but I think the problem is that _primes_between() returns a reference that the code on line 31 is trying to make a copy of.我不是 100% 确定,但我认为问题在于_primes_between()返回第 31 行代码试图复制的引用。 (by taking ownership with the * operator) You could fix the problem by calling .clone() on the result, but I think in this case you don't need _primes_between() to return a value - you can just add the appropriate entries to the v parameter instead. (通过使用*运算符获得所有权)您可以通过对结果调用.clone()来解决问题,但我认为在这种情况下您不需要_primes_between()来返回值 - 您只需添加适当的条目改为v参数。 Something like就像是

fn _primes_between<T>(a: T, b: T, one: T, v: &mut Vec<T>)
where
    T: Copy + Rem<Output = T> + Add<Output = T> + Sub<Output = T> + PartialOrd,
{
    if a <= b {
        if _is_prime(a, a - one, one) {
            v.push(a);
        }

        _primes_between(a + one, b, one, v);
    }
}

fn primes_between<T>(a: T, b: T) -> Vec<T>
where
    T: Copy + Div<Output = T> + Rem<Output = T> + Add<Output = T> + Sub<Output = T> + PartialOrd,
{
    let one = a / a;

    let mut v: Vec<T> = Vec::new();

    _primes_between(a, b, one, &mut v);

    v
}

暂无
暂无

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

相关问题 由于 `slice[_]` 的类型为 `T`,它没有实现 `Copy` 特征,因此无法移出此处发生移动 - Cannot move out of here move occurs because `slice[_]` has type `T`, 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 move 发生是因为 value 的类型为 `RefCell&lt;…&gt;`,它没有实现 `Copy` 特征 - move occurs because value has type `RefCell<…>`, which does not implement the `Copy` trait Rust:发生移动是因为类型为 `ReadDir`,它没有实现 `Copy` 特征 - Rust: move occurs because has type `ReadDir`, which does not implement the `Copy` trait 发生 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 在匹配中分配向量的值:移动发生......它没有实现“复制”特征 - Assigning value of vector inside match: move occurs … which does not implement the `Copy` trait &#39;移动发生是因为值具有类型&#39; Rust 错误 - 'move occurs because value has type' Rust error 无法移出位于共享引用移动后面的 `*handle`,因为 `*handle` 具有类型 - cannot move out of `*handle` which is behind a shared reference move occurs because `*handle` has type `bigdecimal::BigDecimal`,它没有实现 `Copy` 特性 - `bigdecimal::BigDecimal`, which does not implement the `Copy` trait 如何为通用类型Vec的向量实现特征 <T> ? - How to implement a trait for a vector of generic type Vec<T>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM