Skip to content

Instantly share code, notes, and snippets.

@debuti
Forked from rust-play/playground.rs
Last active November 13, 2019 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debuti/46bbaa4c320fc75560b708f558943d12 to your computer and use it in GitHub Desktop.
Save debuti/46bbaa4c320fc75560b708f558943d12 to your computer and use it in GitHub Desktop.
Rust Playground: multiple mutable references to mutexed struct fields
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