我该怎么做才能解决“移动价值的使用"问题?错误? [英] What do I have to do to solve a "use of moved value" error?

查看:61
本文介绍了我该怎么做才能解决“移动价值的使用"问题?错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算 Rust 中的第 10,001 个素数(欧拉项目 7),作为其中的一部分,我检查整数是否是素数的方法引用了一个向量:

I'm trying to compute the 10,001st prime in Rust (Project Euler 7), and as a part of this, my method to check whether or not an integer is prime references a vector:

fn main() {
    let mut count: u32 = 1;
    let mut num: u64 = 1;
    let mut primes: Vec<u64> = Vec::new();
    primes.push(2);

    while count < 10001 {
        num += 2;
        if vectorIsPrime(num, primes) {
            count += 1;
            primes.push(num);
        }
    }
}

fn vectorIsPrime(num: u64, p: Vec<u64>) -> bool {
    for i in p {
        if num > i && num % i != 0 {
            return false;
        }
    }

    true
}

当我尝试引用向量时,出现以下错误:

When I try to reference the vector, I get the following error:

error[E0382]: use of moved value: `primes`
 --> src/main.rs:9:31
  |
9 |         if vectorIsPrime(num, primes) {
  |                               ^^^^^^ value moved here, in previous iteration of loop
  |
  = note: move occurs because `primes` has type `std::vec::Vec<u64>`, which does not implement the `Copy` trait

我必须对 primes 做什么才能在 vectorIsPrime 函数中访问它?

What do I have to do to primes in order to be able to access it within the vectorIsPrime function?

推荐答案

使用您的函数 vectorIsPrime() 的当前定义,该函数指定它需要参数的所有权,因为您传递了它按价值.

With the current definition of your function vectorIsPrime(), the function specifies that it requires ownership of the parameter because you pass it by value.

当一个函数需要一个值参数时,编译器会通过检查它是否实现了特性Copy来检查该值是否可以被复制.

When a function requires a parameter by value, the compiler will check if the value can be copied by checking if it implements the trait Copy.

  • 如果是,则复制该值(使用 memcpy)并提供给该函数,您仍然可以继续使用原始值.
  • 如果没有,则将该值移动到给定的函数,并且调用者之后无法使用它

这就是您收到的错误消息的含义.

That is the meaning of the error message you have.

但是,大多数函数不需要参数的所有权:它们可以处理借用引用",这意味着它们实际上并不拥有该值(并且不能例如将其放入容器中或销毁它).

However, most functions do not require ownership of the parameters: they can work on "borrowed references", which means they do not actually own the value (and cannot for example put it in a container or destroy it).

fn main() {
    let mut count: u32 = 1;
    let mut num: u64 = 1;
    let mut primes: Vec<u64> = Vec::new();
    primes.push(2);

    while count < 10001 {
        num += 2;
        if vector_is_prime(num, &primes) {
            count += 1;
            primes.push(num);
        }
    }
}

fn vector_is_prime(num: u64, p: &[u64]) -> bool {
    for &i in p {
        if num > i && num % i != 0 {
            return false;
        }
    }
    true
}

函数 vector_is_prime() 现在指定它只需要一个 slice,即可以从向量中获得的指向数组(包括其大小)的借用指针使用借用运算符 &.

The function vector_is_prime() now specifies that it only needs a slice, i.e. a borrowed pointer to an array (including its size) that you can obtain from a vector using the borrow operator &.

有关所有权的更多信息,我邀请您阅读本书中关于 所有权.

For more information about ownership, I invite you to read the part of the book dealing with ownership.

这篇关于我该怎么做才能解决“移动价值的使用"问题?错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆