SHARE
TWEET

Untitled

a guest Jun 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn foo<T: Clone>(slices: &[&[T]]) -> Vec<T> {
  2.     slices
  3.         .iter()
  4.         .map(|s| s.iter().cloned())
  5.         .flatten()
  6.         .collect::<Vec<T>>()
  7. }
  8.  
  9. fn bar<T: Clone>(slices: &[&[T]]) -> Vec<T> {
  10.     let n: usize = slices.iter().fold(0, |acc, s| acc + s.len());
  11.     let mut vec = Vec::with_capacity(n);
  12.     slices.iter().for_each(|s| {
  13.         vec.extend_from_slice(s);
  14.     });
  15.     vec
  16. }
  17.  
  18. fn main() {
  19.     let a: [u8; 4] = 42u32.to_le_bytes();
  20.     let v: Vec<u8> = vec![1, 2, 3, 4, 5, 6];
  21.  
  22.     let j: Vec<u8> = foo(&[&a, &v, &a, &a, &a, &v]);
  23.     println!("{:?}", j);
  24.  
  25.     let j2: Vec<u8> = bar(&[&a, &v, &a, &a, &a, &v]);
  26.     println!("{:?}", j2);
  27. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top