Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Which one is better ? global_state
fn change(global_state: &mut u32) {
*global_state += 1;
}
fn main() {
let mut global_state = 0u32;
change(&mut global_state);
println!("Final state: {}",
global_state);
}
use std::sync::Mutex;
static ARRAY: Mutex<Vec<i32>> = Mutex::new(Vec::new());
fn do_a_call() {
ARRAY.lock().unwrap().push(1);
}
fn main() {
do_a_call();
do_a_call();
do_a_call();
let array = ARRAY.lock().unwrap();
println!("Called {} times: {:?}", array.len(), array);
drop(array);
*ARRAY.lock().unwrap() = vec![3, 4, 5];
println!("New singleton object: {:?}", ARRAY.lock(). unwrap());
}
source: Singleton in Rust / Design Patterns
2 posts - 2 participants
🏷️ Rust_feed