Clone Vector in rust except at one index Let's consider you want to clone all items of a vector orig except the item of index i should be taken from alternative value alt.

Lets say the items are of the type

struct Bla{
  // Some fields
}


Using for loop

On simple solution would be:

fn clone(orig: &amp;Vec&lt;Bla&gt;, i: usize, alt: Bla) -&gt; Vec&lt;Bla&gt; {
  let cloned = Vec::new();
  for (j, item) in orig.iter().enumerate() {
    let new_item = if i != j {item.clone()} else {alt};
    cloned.push(new_item);
  }  

  cloned
}



  Note: Does not compile, because the compiler does not know that i==j is only valid once.


Using functional style

The functional alternative would be the follow:

fn clone(orig: &amp;Vec&lt;Bla&gt;, i: usize, alt: Bla) -&gt; Vec&lt;Bla&gt; {
  orig.iter().enumerate()
    .map(|(j, item)| if i != j {item.clone()} else {alt})
    .collect::&lt;Vec&lt;Bla&gt;&gt;()
}


The last one looks nice, but does not compile :( because the rust compiler does not know that alt gets moved (or consumed) only once (i==j).

Using mem replace

In order to fix the version above one can use a default value for the index which should be replaced.

fn clone(orig: &amp;Vec&lt;Bla&gt;, i: usize, alt: Bla) -&gt; Vec&lt;Bla&gt; {
  let mut result = orig.iter().enumerate()
    .map(|(j, item)| if i != j {item.clone()} else {Bla::default()})
    .collect::&lt;Vec&lt;Bla&gt;&gt;();

  mem::replace(&amp;mut result[i], alt);
  result
}


What is the most concise and fastest implementation to do so?
In my opinion none of your solution is idiomatic and all use an useless comparaison.

Using for loop:


Doesn't pre-allocate the vector
Use a comparaison that could be avoid


Using functional style:


Use a comparaison that could be avoid


Using mem replace:


Use a comparaison that could be avoid
Is unnecessary complex to just copy a vector


Also, you should avoid take a &amp;Vec&lt;T&gt; as parameter, it's much better to take a &amp;[T]. See: Why is it discouraged to accept a reference to a String (&amp;String), Vec (&amp;Vec), or Box (&amp;Box) as a function argument?

I would use one of the following version:

#[derive(Clone)]
struct Pokemon {
}

fn a(pokemons: &amp;[Pokemon], i: usize, pokemon: Pokemon) -&gt; Vec&lt;Pokemon&gt; {
    let mut digimons = Vec::with_capacity(pokemons.len());

    digimons.extend_from_slice(&amp;pokemons[0..i]);
    digimons.push(pokemon);
    digimons.extend_from_slice(&amp;pokemons[i + 1..]);

    digimons
}

fn b(pokemons: &amp;[Pokemon], i: usize, pokemon: Pokemon) -&gt; Vec&lt;Pokemon&gt; {
    let mut digimons = pokemons.to_vec();
    digimons[i] = pokemon;

    digimons
}

fn c(pokemons: &amp;[Pokemon], i: usize, pokemon: Pokemon) -&gt; Vec&lt;Pokemon&gt; {
    pokemons[0..i]
        .iter()
        .cloned()
        .chain(std::iter::once(pokemon))
        .chain(pokemons[i + 1..].iter().cloned())
        .collect()
}


I don't really know witch version is better, a() and c() should be the same but sometime chain() is not optimized away. And b() is probably the most straightforward solution. I think I would choice a() in a production code.

However, looking at the assembly, we can see that a() and c() doesn't produce the same, and that b() is the simplest.