Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

rust - How do I create two new mutable slices from one slice?

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.

My attempt #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 #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 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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The split_at and split_at_mut methods will give you two slices, which you can then copy or even safely use without copying if borrow checker allows.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...