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;
#[derive(Debug)]
pub struct Sample {
a: u32,
b: u32,
c: u32,
}
impl Default for Sample {
fn default() -> Self {
Sample { a: 2, b: 4, c: 6}
}
}
#[derive(Debug)]
pub struct Clients {
socket: Vec<Sample>
}
impl Clients {
fn new() -> Self { Clients { socket: Vec::new() } }
fn insert(&mut self, s: Sample) {
self.socket.push(s)
}
}
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() };
clients.insert(s1);
clients.insert(s2);
clients.insert(s3);
println!("trucks: {:#?}", clients);
clients.socket.into_iter()
.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/main.rs:42:31
|
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.