this post was submitted on 15 Nov 2025
6 points (100.0% liked)

Rust

7505 readers
23 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
 

For example, is there any problems with doing this?

fn main() {  
	static mut BUF: [u8; 0x400] = [0; 0x400];  
	let buf = &mut unsafe { BUF };  
}  

and is this code the same as just using an array directly? From my understanding local variables get put on the stack but do the static variables do too?

I'm essentially trying to find the most performant way to get a simple read/write buffer.

you are viewing a single comment's thread
view the rest of the comments
[–] BB_C@programming.dev 2 points 1 week ago (1 children)

What made you reach out to a static mut in the first place?

[–] Val@anarchist.nexus 1 points 1 week ago (1 children)

My logic was simply: I need a buffer that is only initialised once no matter how many times the function is called. statics are initialised at program start so they seemed like a good fit. and since I wasn't planning for the function to me called multiple times simultaneously it seemed like the UB didn't matter. (which I think was correct)

[–] BB_C@programming.dev 1 points 1 week ago

UB didn’t matter.

There is no such a thing.

If you really must pretend this matters performance wise, look up MaybeUninit. It still requires unsafe{}, but it's a lot less trouble.