如何从一个切片创建两个新的可变切片?

use*_*299 7 slice rust

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

我的尝试#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);
Run Code Online (Sandbox Code Playgroud)

输出:

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();
  |                                           ^^^^^
Run Code Online (Sandbox Code Playgroud)

我的尝试#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);
Run Code Online (Sandbox Code Playgroud)

输出:

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()`
Run Code Online (Sandbox Code Playgroud)

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

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);
}
Run Code Online (Sandbox Code Playgroud)

Kor*_*nel 11

split_atsplit_at_mut方法会给你两片,然后你就可以复制,甚至没有安全的,如果借检查允许复制使用.

let (list_a, list_b) = my_list.split_at_mut(my_list.len()/2)
Run Code Online (Sandbox Code Playgroud)


snf*_*snf 6

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

  1. Vec::to_vec
  2. From/Into
  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);
}
Run Code Online (Sandbox Code Playgroud)

您的原始问题是由于所有这些都返回了Vec但是您试图声称它是一个切片,相当于:

let thing: &mut [u8] = Vec::new();
Run Code Online (Sandbox Code Playgroud)