Value borrowed after partial move

I need to collect the group of the Sample struct together, so I created another struct Clients that is of one vec element as shown in the code below.

I need to change the value of b to be 5, wherever the value of a is 23.

So, I tried using the functional style, into_iter().filter().map() as shown as well:

use std::default::Default;

pub struct Sample {
    a: u32,
    b: u32,
    c: u32,

impl Default for Sample {
    fn default() -> Self {
        Sample { a: 2, b: 4, c: 6}

pub struct Clients {
    socket: Vec<Sample>

impl Clients {
    fn new() -> Self { Clients { socket: Vec::new() } }

    fn insert(&mut self, s: Sample) {

fn main() {
    let mut clients = Clients::new();
    let s1 = Sample { .. Sample::default() };
    let s2 = Sample { a: 23, b:2, .. Sample::default() };
    let s3 = Sample { a: 23, .. Sample::default() };
    println!("trucks: {:#?}", clients);

        .filter(|client| client.a == 23)
        .map(|mut s| s.b =5);
    println!("trucks: {:#?}", clients);

But I got the below error:

error[E0382]: borrow of moved value: `clients`
  --> src/
39 |     clients.socket.into_iter()
   |     -------------- value moved here
42 |     println!("trucks: {:#?}", clients);
   |                               ^^^^^^^ value borrowed here after partial move
   = note: move occurs because `clients.socket` has type `std::vec::Vec<Sample>`, which does not implement the `Copy` trait

The playground shows this gist.

Instead of .into_iter() you want to use iter_mut(), and then instead of .map you can use for_each (map is lazy and does not force the iteration).


You get this error, because into_* functions destroy the object they’ve been called on, so Clients has a “hole” in the place where socket was, so it’s not a usable object any more.

1 Like

Appreciate your explanation.