Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Filter vector inside MutexGuard without cloning

$
0
0

I have a locked a with a vecotr of B struct inside. I'd like to filter a.b without cloning B struct. Is it possible in Rust?

use std::sync::{Arc, Mutex};#[derive(Debug)]struct B {    n: i64,}#[derive(Debug)]struct A {    b: Vec<B>}fn main() {    let arc_a = Arc::new(Mutex::new(A {        b: vec![            B { n: 1 },            B { n: 2 },            B { n: 3 },            B { n: 4 },        ]    }));    let mut a = arc_a.lock().unwrap();    a.b = a.b.into_iter().filter(|b| b.n % 2 == 1).collect();    println!("{:?}", a);}

In the code example above I have an error error[E0507]: cannot move out of dereference of MutexGuard<'_, A>. Are there ways to solve this challenge?


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>