-
-
Save debuti/46bbaa4c320fc75560b708f558943d12 to your computer and use it in GitHub Desktop.
Rust Playground: multiple mutable references to mutexed struct fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::{Arc, Mutex}; | |
pub struct Foo { | |
pub a: i32, | |
pub b: i32, | |
} | |
pub fn function(foo: Arc<Mutex<Foo>>){ | |
let foo = &mut *(foo.lock().unwrap()); | |
inner(&mut foo.a, &mut foo.b); | |
} | |
fn inner(a: &mut i32, b:&mut i32){ | |
*a += 1; | |
*b += *a; | |
} | |
fn main(){ | |
let x = Arc::new(Mutex::new(Foo{a :10, b: 20})); | |
function(x.clone()); | |
function(x.clone()); | |
let x = x.lock().unwrap(); | |
println!("{} {}", x.a, x.b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment