如何从一个切片创建两个新的可变切片? [英] How do I create two new mutable slices from one slice?

查看:58
本文介绍了如何从一个切片创建两个新的可变切片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个可变切片并将内容复制到两个新的可变切片中.每个切片都是原始切片的一半.

I would like to take a mutable slice and copy the contents into two new mutable slices. Each slice being one half of the original.

我的尝试 #1:

let my_list: &mut [u8] = &mut [0, 1, 2, 3, 4, 5];
let list_a: &mut [u8] = my_list[0..3].clone();
let list_b: &mut [u8] = my_list[3..6].clone();
println!("{:?}", my_list);
println!("{:?}", list_a);
println!("{:?}", list_b);

输出:

error: no method named `clone` found for type `[u8]` in the current scope
 --> src/main.rs:3:43
  |
3 |     let list_a: &mut [u8] = my_list[0..3].clone();
  |                                           ^^^^^

error: no method named `clone` found for type `[u8]` in the current scope
 --> src/main.rs:4:43
  |
4 |     let list_b: &mut [u8] = my_list[3..6].clone();
  |                                           ^^^^^

我的尝试 #2:

let my_list: &mut [u8] = &mut [0, 1, 2, 3, 4, 5];
let list_a: &mut [u8] = my_list[0..3].to_owned();
let list_b: &mut [u8] = my_list[3..6].to_owned();
println!("{:?}", my_list);
println!("{:?}", list_a);
println!("{:?}", list_b);

输出:

error[E0308]: mismatched types
  --> src/main.rs:12:29
   |
12 |     let list_a: &mut [u8] = my_list[0..3].to_owned();
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^ expected &mut [u8], found struct `std::vec::Vec`
   |
   = note: expected type `&mut [u8]`
              found type `std::vec::Vec<u8>`
   = help: try with `&mut my_list[0..3].to_owned()`

error[E0308]: mismatched types
  --> src/main.rs:13:29
   |
13 |     let list_b: &mut [u8] = my_list[3..6].to_owned();
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^ expected &mut [u8], found struct `std::vec::Vec`
   |
   = note: expected type `&mut [u8]`
              found type `std::vec::Vec<u8>`
   = help: try with `&mut my_list[3..6].to_owned()`

我可以使用两个 Vec 并且只是循环输入并推送我猜的克隆值,但我希望有更好的方法来做到这一点:

I can use two Vec<u8> and just loop over the input and push cloned values I guess, but I was hoping there was a nicer way to do this:

extern crate rand;

use rand::{thread_rng, Rng};

fn main() {
    let my_list: &mut [u8] = &mut [0; 100];
    thread_rng().fill_bytes(my_list);
    let list_a = &mut Vec::new();
    let list_b = &mut Vec::new();
    for i in 0..my_list.len() {
        if i < my_list.len() / 2 {
            list_a.push(my_list[i].clone());
        } else {
            list_b.push(my_list[i].clone());
        }
    }
    println!("{:?}", list_a.as_slice());
    println!("{:?}", list_b.as_slice());
    println!("{:?}", my_list);
}

推荐答案

您可以通过使用多种方法克隆元素来直接从切片构建向量:

You can build vectors from slices directly by cloning the elements using multiple methods:

  1. Vec::to_vec
  2. From/进入
  3. ToOwned

fn main() {
    let my_list: &mut [u8] = &mut [0, 1, 2, 3, 4, 5];
    let mut vec1 = my_list[0..2].to_vec();
    let mut vec2: Vec<u8> = my_list[2..4].into();
    let mut vec3 = my_list[2..6].to_owned();

    println!("{:?}", vec1);
    println!("{:?}", vec2);
}

你最初的问题是因为所有这些都返回一个 Vec 但你试图声称它是一个切片,相当于:

Your original problem was caused because all of these return a Vec but you were attempting to claim that it was a slice, equivalent to:

let thing: &mut [u8] = Vec::new();

这篇关于如何从一个切片创建两个新的可变切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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