Better way to declare uninitialized variables

2020-04-19 06:08发布

问题:

Some libc functions, e.g. sigemptyset(set: *mut sigset_t) take a pointer to a variable, treat it as uninitialized and initialize it.

I end up with this code:

let mut newmask = std::mem::uninitialized();
libc::sigemptyset(&mut newmask);

This is ok, but when I have many of those variables I end up with something like this:

let mut newmask = std::mem::uninitialized();
let mut oldmask = std::mem::uninitialized();
let mut pendmask = std::mem::uninitialized();

I could condense it:

use std::mem::unitialized as uninit;
let (mut newmask, mut oldmask, mut pendmask) = (uninit(), uninit(), uninit());

Is there a nicer way to write this code? For educational purposes I explicitly want to use libc.

回答1:

Luckily, tuples are normal types, too. So how about:

let (mut newmask, mut oldmask, mut pendmask) = std::mem::uninitialized();

However, it won't get much nicer than this. Your best bet is to combine all your variables into a bigger type (like a tuple or a struct) and un-initialize that.

But it's fine that unsafe things are verbose and annoying to write. Uninitialized variables are really quite dangerous, especially when dealing with Drop types. I am sure you are already aware, but I still want to make sure everyone reads the documentation of uninitialized() to understand all possible pitfalls.