How to create two new mutable fragments from one fragment?

I would like to take a mutable fragment and copy the contents into two new mutable fragments. Each fragment is half the original.

My attempt number 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); 

Output:

 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(); | ^^^^^ 

My attempt number 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); 

Output:

 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()` 

I can use two Vec<u8> and just loop over the input and push the cloned values, I think, but I was hoping there would be a better 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); } 
+7
slice rust
source share
3 answers

You can create vectors from slices directly by cloning elements using several methods:

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

The original problem was caused by the fact that they all return Vec , but you tried to claim that this is a fragment equivalent to:

 let thing: &mut [u8] = Vec::new(); 
+5
source share

split_at and split_at_mut methods will provide you with two snippets that you can then copy or even use safely without copying, if the checker tool allows you to.

 let (list_a, list_b) = my_list.split_at_mut(my_list.len()/2) 
+10
source share

You can link two iterators over slices.

 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 will iterate over the first iterator, then the second.

To create new lists:

 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(); 
+2
source share

All Articles