Is there an optimization difference between UnsafeCell and *mut T? When should they be used, and what should be kept in mind?

⚓ Rust    📅 2026-03-02    👤 surdeus    👁️ 2      

surdeus

Do both UnsafeCell and *mut T reach the same optimization level, or does *mut T offer higher optimization than UnsafeCell?

When should I use UnsafeCell and *mut T? My current understanding is that UnsafeCell allows mutating a value via &self, whereas *mut T requires &mut self. What else is there?

Furthermore, what are the boundaries or constraints I must observe to avoid undefined behavior?

8 posts - 4 participants

Read full topic

🏷️ Rust_feed