如果都是vector,可以用vector自带的append方法 或者 Extend trait 或者slice的concat方法
fn main() { let mut a = vec![1, 2, 3]; let mut b = vec![4, 5, 6]; a.append(&mut b); assert_eq!(a, [1, 2, 3, 4, 5, 6]); assert_eq!(b, []); } 或者 fn main() { let mut a = vec![1, 2, 3]; let b = vec![4, 5, 6]; a.extend(b); assert_eq!(a, [1, 2, 3, 4, 5, 6]); } 或者 fn main() { let mut a = vec![1, 2, 3]; let b = vec![4, 5, 6]; [a, b].concat() assert_eq!(a, [1, 2, 3, 4, 5, 6]); }
用extend_from_slice
fn cat<T: Clone>(a: &[T], b: &[T]) -> Vec<T> {
let mut v = Vec::with_capacity(a.len() + b.len());
v.extend_from_slice(a);
v.extend_from_slice(b);
v
} 或者
fn join<T: Clone>(a: &[T], b: &[T]) -> Vec<T> { a.iter().cloned().chain(b.iter().cloned()).collect() }
或者 fn join<T: Clone>(a: &[T], b: &[T]) -> Vec<T> { let mut v = a.to_vec(); v.extend_from_slice(b); v }