Best way to concatenate vectors in Rust

前端 未结 3 758
渐次进展
渐次进展 2020-12-01 09:55

Is it even possible to concatenate vectors in Rust? If so, is there an elegant way to do so? I have something like this:

let mut a = vec![1, 2, 3];
let b = v         


        
3条回答
  •  鱼传尺愫
    2020-12-01 10:39

    I can't make it in one line. Damian Dziaduch

    It is possible to do it in one line by using chain():

    let c: Vec = a.into_iter().chain(b.into_iter()).collect(); // Consumed
    let c: Vec<&i32> = a.iter().chain(b.iter()).collect(); // Referenced
    let c: Vec = a.iter().cloned().chain(b.iter().cloned()).collect(); // Cloned
    let c: Vec = a.iter().copied().chain(b.iter().copied()).collect(); // Copied
    

    There are infinite ways.

提交回复
热议问题