简体   繁体   English

在匹配中分配向量的值:移动发生......它没有实现“复制”特征

[英]Assigning value of vector inside match: move occurs … which does not implement the `Copy` trait

For convenience, you can find the below code on Rust Playground :为方便起见,您可以在Rust Playground上找到以下代码:

fn main() {
    println!("Hello, world!");
    let n = 5;
    let mut my_vector : Vec<u16>;
    
    loop {
        let x: Result<i32, &str> = Ok(-3);
        match x {
            Ok(-3) => {
                my_vector = (0..n).map(|x| x).collect();
                // works
                for e in my_vector {
                    println!("{}", e);
                }
            },
            Err(e) => {
                eprintln!("couldn't recieve a datagram: {}", e);
            },
            _ => {
            }
        }
        // does not work
        for e in my_vector {
            println!("{}", e);
        }
        break;
    }
}

I get the error " move occurs because my_vector has type Vec<u16>, which does not implement the Copy trait " and I am not able to make the program compile (and work), any help very well appreciated!我收到错误“ move occurs because my_vector has type Vec<u16>, which does not implement the Copy trait ”,并且我无法使程序编译(和工作),非常感谢任何帮助!

NB: This is a simplified example of the error I am facing in my program.注意:这是我在程序中遇到的错误的简化示例。

How could I set the value of the vector inside the match {} and then reuse it later on?如何在 match {} 中设置向量的值,然后再使用它?

for e in my_vector {
    println!("{}", e);
}

This consumes my_vector .这会消耗my_vector If you want to iterate the elements and still be able to use them later, you can use .iter()如果你想迭代元素并且以后仍然可以使用它们,你可以使用.iter()

for e in my_vector.iter() {
    println!("{}", e);
}

Here's a working playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8c99592ef98735472165f76b6ceb9b19这是一个工作场所: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8c99592ef98735472165f76b6ceb9b19

暂无
暂无

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

相关问题 移动发生是因为值的类型为 Vec<T> ,它没有实现 `Copy` 特性 - move occurs because value has type Vec<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 由于 `slice[_]` 的类型为 `T`,它没有实现 `Copy` 特征,因此无法移出此处发生移动 - Cannot move out of here move occurs because `slice[_]` has type `T`, which does not implement the `Copy` trait Rust:发生移动是因为类型为 `ReadDir`,它没有实现 `Copy` 特征 - Rust: move occurs because has type `ReadDir`, 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 `bigdecimal::BigDecimal`,它没有实现 `Copy` 特性 - `bigdecimal::BigDecimal`, which does not implement the `Copy` trait 如何强制执行实现复制特征的类型的移动? - How to force a move of a type which implements the Copy trait? 为另一个特征实现一个特征是什么意思? - What does it mean to implement a trait for another trait? 为什么“ impl Trait”返回值实现“ Box时发送” <dyn Trait> `不是吗? - Why does an `impl Trait` return value implement Send while `Box<dyn Trait>` does not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM