2014-07-21 52 views
7

我想採取一個可變片,並將內容複製到兩個新的可變片。每片是原來的一半。如何從一個切片創建兩個新的可變片?

我嘗試#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<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); 
} 

回答

5

可以建立從片載體::

要創建新的列表

  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); 
} 

你原來的問題造成的,因爲所有的這些返回Vec但你試圖聲稱這是一個切片,相當於:

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

不錯,我沒有意識到這個功能的存在。 –

+0

不錯,絕對是我目前看到的最乾淨的。使用Vec vs&[]或者&mut []有什麼不利之處?很明顯,Vec很容易處理,但似乎可能會有一些開銷。 – user439299

+1

切片只是一種表示形式,如果調用as_slice(),它將借用對矢量的引用,但不會創建新的。如果有疑問,請檢查生命週期參數(http://doc.rust-lang.org/std/vec/struct.Vec.html#method.as_slice),它具有與向量相同的生命週期。 – snf

2

你可以鏈兩個迭代器在切片。

let my_list: &mut [u8] = &mut [0, 1, 2, 3, 4, 5]; 
let mut slices = my_list[0..3].iter().chain(my_list[3..6].iter()); 
for e in slices {} 

chain將遍歷第一迭代,則第二。直接通過使用多種方法克隆該元件

let my_list: &mut [u8] = &mut [0, 1, 2, 3, 4, 5]; 
let mut a: Vec<u8> = my_list[0..3].iter().cloned().collect(); 
let mut b: Vec<u8> = my_list[3..6].iter().cloned().collect(); 
+0

正在循環切片而不是循環遍歷my_list?我試圖結束除原來的兩個新列表。 – user439299

+0

如果你想要新的列表,創建兩個向量。切片只是一些向量的視圖。 –

+0

當我嘗試連接iter()和slice()時,我得到這個:**未能找到trait core的實現:: iter :: FromIterator for&mut [u8] ** ...編輯:nvm忘記添加Vec user439299

10

split_atsplit_at_mut方法會給你有兩個切片,如果借用檢查器允許,你可以複製或者甚至可以安全地使用,而不需要複製。

let (list_a, list_b) = my_list.split_at_mut(my_list.len()/2)