How to mutate a Thread Local RefCell?

⚓ rust    📅 2025-05-09    👤 surdeus    👁️ 2      

surdeus

/*
1. Why does the second println! output print LocalKey{ .. }
   instead of the same output as the the first println! ?
2. If you remove the // below, why does it not compile ?
*/
use std::cell::RefCell;
use std::ops::DerefMut;

thread_local! { 
    static VEC: RefCell< Vec<f32> > = RefCell::new( vec![1.0, 2.0, 3.0] );
}

fn main() { 
    let v: RefCell< Vec<f32> > = RefCell::new( vec![1.0, 2.0] );
    v.borrow_mut( ).deref_mut().push(3.0);
    println!( "{:?}", v  );

    // VEC.borrow_mut( ).deref_mut().push(3.0);
    println!( "{:?}", VEC  );
}

3 posts - 2 participants

Read full topic

🏷️ rust_feed